Skip to content

Reduce the amount of content served from files in tests. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions internal/pull/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ import (
const initialActionRepository = "./pull_test/codeql-action-initial.git"
const modifiedActionRepository = "./pull_test/codeql-action-modified.git"

const releaseSomeCodeQLVersionOnMainContent = "This isn't really a CodeQL bundle!"

var releaseSomeCodeQLVersionOnMain = github.RepositoryRelease{
TagName: github.String("some-codeql-version-on-main"),
Name: github.String("some-codeql-version-on-main"),
Assets: []*github.ReleaseAsset{
&github.ReleaseAsset{
ID: github.Int64(1),
Name: github.String("codeql-bundle.tar.gz"),
Size: github.Int(len(releaseSomeCodeQLVersionOnMainContent)),
},
},
}

const releaseSomeCodeQLVersionOnV1AndV2Content = "This isn't a CodeQL bundle either, but it's a different not-bundle."

var releaseSomeCodeQLVersionOnV1AndV2 = github.RepositoryRelease{
TagName: github.String("some-codeql-version-on-v1-and-v2"),
Name: github.String("some-codeql-version-on-v1-and-v2"),
Assets: []*github.ReleaseAsset{
&github.ReleaseAsset{
ID: github.Int64(2),
Name: github.String("codeql-bundle.tar.gz"),
Size: github.Int(len(releaseSomeCodeQLVersionOnV1AndV2Content)),
},
},
}

func getTestPullService(t *testing.T, temporaryDirectory string, gitCloneURL string, githubURL string) pullService {
cacheDirectory := cachedirectory.NewCacheDirectory(temporaryDirectory)
var githubDotComClient *github.Client
Expand Down Expand Up @@ -123,43 +151,43 @@ func TestPullReleases(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
githubTestServer, githubURL := test.GetTestHTTPServer(t)
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-main.json", response)
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnMain, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/1", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-main.bin", response)
test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnMainContent, response)
}).Methods("GET").Headers("accept", "application/octet-stream")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-v1-and-v2.json", response)
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnV1AndV2, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/2", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", response)
test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnV1AndV2Content, response)
}).Methods("GET").Headers("accept", "application/octet-stream")
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL)
err := pullService.pullGit(true)
require.NoError(t, err)
err = pullService.pullReleases()
require.NoError(t, err)

test.RequireFilesAreEqual(t, "./pull_test/api/asset-some-codeql-version-on-main.bin", pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz"))
test.RequireFilesAreEqual(t, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"))
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnMainContent, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz"))
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnV1AndV2Content, pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"))

// If we pull again, we should only download assets where the size mismatches.
err = ioutil.WriteFile(pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"), []byte("Some nonsense."), 0644)
require.NoError(t, err)
githubTestServer, githubURL = test.GetTestHTTPServer(t)
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-main.json", response)
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnMain, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-v1-and-v2.json", response)
test.ServeHTTPResponseFromObject(t, releaseSomeCodeQLVersionOnV1AndV2, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/2", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", response)
test.ServeHTTPResponseFromString(t, releaseSomeCodeQLVersionOnV1AndV2Content, response)
}).Methods("GET").Headers("accept", "application/octet-stream")
pullService = getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL)
err = pullService.pullReleases()
require.NoError(t, err)

test.RequireFilesAreEqual(t, "./pull_test/api/asset-some-codeql-version-on-main.bin", pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz"))
test.RequireFilesAreEqual(t, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"))
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnMainContent, pullService.cacheDirectory.AssetPath("some-codeql-version-on-main", "codeql-bundle.tar.gz"))
test.RequireFileHasContent(t, releaseSomeCodeQLVersionOnV1AndV2Content, pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"))
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

18 changes: 9 additions & 9 deletions internal/push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func TestCreateRepositoryWhenUserIsOwner(t *testing.T) {
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-owner.json", response)
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
response.WriteHeader(http.StatusNotFound)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/user/repos", func(response http.ResponseWriter, request *http.Request) {
response.Write([]byte("{}"))
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
}).Methods("POST")
_, err := pushService.createRepository()
require.NoError(t, err)
Expand All @@ -61,13 +61,13 @@ func TestUpdateRepositoryWhenUserIsOwner(t *testing.T) {
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-owner.json", response)
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.Repository{Homepage: github.String(repositoryHomepage)}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
response.Write([]byte("{}"))
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
}).Methods("PATCH")
_, err := pushService.createRepository()
require.NoError(t, err)
Expand All @@ -78,7 +78,7 @@ func TestUpdateRepositoryWhenUserIsOwnerForced(t *testing.T) {
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-owner.json", response)
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
Expand All @@ -99,24 +99,24 @@ func TestCreateOrganizationAndRepositoryWhenOrganizationIsOwner(t *testing.T) {
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
organizationCreated := false
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromFile(t, http.StatusOK, "./push_test/api/user-is-not-owner.json", response)
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("user")}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/orgs/destination-repository-owner", func(response http.ResponseWriter, request *http.Request) {
if organizationCreated {
response.Write([]byte("{}"))
test.ServeHTTPResponseFromObject(t, github.Organization{}, response)
} else {
response.WriteHeader(http.StatusNotFound)
}
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/admin/organizations", func(response http.ResponseWriter, request *http.Request) {
response.Write([]byte("{}"))
test.ServeHTTPResponseFromObject(t, github.Organization{}, response)
organizationCreated = true
}).Methods("POST")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
response.WriteHeader(http.StatusNotFound)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/orgs/destination-repository-owner/repos", func(response http.ResponseWriter, request *http.Request) {
response.Write([]byte("{}"))
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
}).Methods("POST")
_, err := pushService.createRepository()
require.NoError(t, err)
Expand Down
3 changes: 0 additions & 3 deletions internal/push/push_test/api/user-is-not-owner.json

This file was deleted.

3 changes: 0 additions & 3 deletions internal/push/push_test/api/user-is-owner.json

This file was deleted.

13 changes: 4 additions & 9 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ func GetTestHTTPServer(t *testing.T) (*mux.Router, string) {
return mux, server.URL
}

func ServeHTTPResponseFromFile(t *testing.T, statusCode int, path string, response http.ResponseWriter) {
data, err := ioutil.ReadFile(path)
require.NoError(t, err)
response.WriteHeader(statusCode)
_, err = response.Write(data)
func ServeHTTPResponseFromString(t *testing.T, content string, response http.ResponseWriter) {
_, err := response.Write([]byte(content))
require.NoError(t, err)
}

Expand All @@ -51,12 +48,10 @@ func ServeHTTPResponseFromObject(t *testing.T, object interface{}, response http
require.NoError(t, err)
}

func RequireFilesAreEqual(t *testing.T, expectedPath string, actualPath string) {
expectedData, err := ioutil.ReadFile(expectedPath)
require.NoError(t, err)
func RequireFileHasContent(t *testing.T, expectedContent string, actualPath string) {
actualData, err := ioutil.ReadFile(actualPath)
require.NoError(t, err)
require.Equal(t, expectedData, actualData)
require.Equal(t, []byte(expectedContent), actualData)
}

func CheckExpectedReferencesInRepository(t *testing.T, repositoryPath string, expectedReferences []string) {
Expand Down