-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
342 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,192 @@ | ||
package uploader | ||
|
||
import ( | ||
"math" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/ubuntu/ubuntu-insights/internal/constants" | ||
"github.com/ubuntu/ubuntu-insights/internal/fileutils" | ||
) | ||
|
||
type mockTimeProvider struct { | ||
currentTime int64 | ||
} | ||
|
||
func (m mockTimeProvider) NowUnix() int64 { | ||
return m.currentTime | ||
} | ||
|
||
func TestUploadBadFile(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := map[string]struct { | ||
fName string | ||
fileContents string | ||
missingFile bool | ||
fileIsDir bool | ||
url string | ||
consent bool | ||
minAge uint | ||
|
||
wantErr bool | ||
}{ | ||
"Ok": {fName: "0.json", fileContents: `{"Content":true, "string": "string"}`, wantErr: false}, | ||
"Missing File": {fName: "0.json", fileContents: `{"Content":true, "string": "string"}`, missingFile: true, wantErr: true}, | ||
"File Is Dir": {fName: "0.json", fileIsDir: true}, | ||
"Non-numeric": {fName: "not-numeric.json", fileContents: `{"Content":true, "string": "string"}`, wantErr: true}, | ||
"Bad File Ext": {fName: "0.txt", fileContents: `{"Content":true, "string": "string"}`}, | ||
"Bad Contents": {fName: "0.json", fileContents: `bad content`}, | ||
"minAge Overflow": {fName: "0.json", fileContents: `{"Content":true, "string": "string"}`, minAge: math.MaxUint64, wantErr: true}, | ||
"Bad URL": {fName: "0.json", fileContents: `{"Content":true, "string": "string"}`, url: "http://bad host:1234", wantErr: true}, | ||
|
||
"Ok Consent": {fName: "0.json", fileContents: `{"Content":true, "string": "string"}`, consent: true, wantErr: false}, | ||
"Missing File Consent": {fName: "0.json", fileContents: `{"Content":true, "string": "string"}`, missingFile: true, consent: true, wantErr: true}, | ||
"File Is Dir Consent": {fName: "0.json", fileIsDir: true, consent: true, wantErr: true}, | ||
"Non-numeric Consent": {fName: "not-numeric.json", fileContents: `{"Content":true, "string": "string"}`, consent: true, wantErr: true}, | ||
"Bad File Ext Consent": {fName: "0.txt", fileContents: `{"Content":true, "string": "string"}`, consent: true}, | ||
"Bad Contents Consent": {fName: "0.json", fileContents: `bad content`, consent: true, wantErr: true}, | ||
"minAge Overflow Consent": {fName: "0.json", fileContents: `{"Content":true, "string": "string"}`, minAge: math.MaxUint64, consent: true, wantErr: true}, | ||
"Bad URL Consent": {fName: "0.json", fileContents: `{"Content":true, "string": "string"}`, url: "http://bad host:1234", consent: true, wantErr: true}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
require.False(t, tc.missingFile && tc.fileIsDir, "Test case cannot have both missing file and file is dir") | ||
|
||
dir := t.TempDir() | ||
|
||
um := &Manager{ | ||
collectedDir: filepath.Join(dir, constants.LocalFolder), | ||
uploadedDir: filepath.Join(dir, constants.UploadedFolder), | ||
minAge: tc.minAge, | ||
timeProvider: mockTimeProvider{currentTime: 0}, | ||
} | ||
|
||
require.NoError(t, os.Mkdir(um.collectedDir, 0750), "Setup: failed to create uploaded folder") | ||
require.NoError(t, os.Mkdir(um.uploadedDir, 0750), "Setup: failed to create collected folder") | ||
fPath := filepath.Join(um.collectedDir, tc.fName) | ||
|
||
if !tc.missingFile && !tc.fileIsDir { | ||
require.NoError(t, fileutils.AtomicWrite(fPath, []byte(tc.fileContents)), "Setup: failed to create report file") | ||
} | ||
if tc.fileIsDir { | ||
require.NoError(t, os.Mkdir(fPath, 0750), "Setup: failed to create directory") | ||
} | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
t.Cleanup(func() { ts.Close() }) | ||
|
||
if tc.url == "" { | ||
tc.url = ts.URL | ||
} | ||
|
||
err := um.upload(tc.fName, tc.url, tc.consent) | ||
if tc.wantErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestMoveReport(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := map[string]struct { | ||
fileExists bool | ||
|
||
wantErr bool | ||
}{ | ||
"File Exists": {fileExists: true}, | ||
|
||
"File Not Found": {fileExists: false, wantErr: true}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
dir := t.TempDir() | ||
|
||
um := &Manager{ | ||
collectedDir: filepath.Join(dir, constants.LocalFolder), | ||
uploadedDir: filepath.Join(dir, constants.UploadedFolder), | ||
} | ||
|
||
require.NoError(t, os.MkdirAll(um.collectedDir, 0750), "Setup: failed to create uploaded folder") | ||
require.NoError(t, os.MkdirAll(um.uploadedDir, 0750), "Setup: failed to create collected folder") | ||
|
||
if tc.fileExists { | ||
f, err := os.Create(filepath.Join(um.collectedDir, "report.json")) | ||
require.NoError(t, err) | ||
f.Close() | ||
} | ||
|
||
err := um.moveReport("report.json", []byte("payload")) | ||
if tc.wantErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
|
||
_, err = os.Stat(filepath.Join(um.uploadedDir, "report.json")) | ||
if !tc.fileExists { | ||
require.Error(t, err, "File should not exist in the uploaded directory") | ||
return | ||
} | ||
|
||
require.NoError(t, err, "File should exist in the uploaded directory") | ||
}) | ||
} | ||
} | ||
|
||
func TestSend(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := map[string]struct { | ||
url string | ||
noServer bool | ||
serverResponse int | ||
|
||
wantErr bool | ||
}{ | ||
"No Server": {noServer: true, wantErr: true}, | ||
"Bad URL": {url: "http://local host:1234", serverResponse: http.StatusOK, wantErr: true}, | ||
"Bad Response": {serverResponse: http.StatusForbidden, wantErr: true}, | ||
|
||
"Success": {serverResponse: http.StatusOK}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(tc.serverResponse) | ||
})) | ||
t.Cleanup(func() { ts.Close() }) | ||
|
||
if tc.url == "" { | ||
tc.url = ts.URL | ||
} | ||
if tc.noServer { | ||
ts.Close() | ||
} | ||
|
||
err := send(tc.url, []byte("payload")) | ||
if tc.wantErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
internal/uploader/testdata/TestUpload/golden/consent_manager_false
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
uploaded/1.json: '{"OptOut":true}' | ||
uploaded/1.json: '{"OptOut": true}' |
2 changes: 1 addition & 1 deletion
2
internal/uploader/testdata/TestUpload/golden/consent_manager_global_false,_source_true
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
uploaded/1.json: '{"OptOut":true}' | ||
uploaded/1.json: '{"OptOut": true}' |
2 changes: 1 addition & 1 deletion
2
internal/uploader/testdata/TestUpload/golden/consent_manager_global_true,_source_false
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
uploaded/1.json: '{"OptOut":true}' | ||
uploaded/1.json: '{"OptOut": true}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.