diff --git a/.github/workflows/codeformat.yaml b/.github/workflows/codeformat.yaml index c75772ab..74105dd5 100644 --- a/.github/workflows/codeformat.yaml +++ b/.github/workflows/codeformat.yaml @@ -1,4 +1,4 @@ -name: pull_request +name: Code format check on: pull_request jobs: codegen: @@ -6,11 +6,12 @@ jobs: runs-on: ubuntu-22.04 steps: - name: Cancel Previous Actions - uses: styfle/cancel-workflow-action@0.11.0 + uses: styfle/cancel-workflow-action@0.12.1 with: access_token: ${{ github.token }} - name: Free disk space run: | + : Free disk space echo "Before clearing disk space:" df -h @@ -27,11 +28,11 @@ jobs: echo "After clearing disk space:" df -h - name: Check out code into the Go module directory - uses: actions/checkout@v3 + uses: actions/checkout@v5 with: fetch-depth: 0 - name: Set up Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v6 with: go-version-file: "go.mod" id: go diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 7083553d..97f91da2 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -1,4 +1,4 @@ -name: pull_request +name: Tests on: push: @@ -13,43 +13,51 @@ jobs: runs-on: ubuntu-22.04 steps: - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.11.0 + uses: styfle/cancel-workflow-action@0.12.1 with: access_token: ${{ github.token }} - - uses: actions/checkout@v3 + - uses: actions/checkout@v5 with: fetch-depth: 0 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v6 with: python-version: 3.8 - - uses: google-github-actions/setup-gcloud@a48b55b3b0eeaf77b6e1384aab737fbefe2085ac + + - name: Gcloud login + if: ${{ env.has_auth }} + uses: google-github-actions/auth@v2 with: - version: '290.0.1' project_id: ${{ secrets.GCP_PROJECT_ID }} - service_account_key: ${{ secrets.GCP_SA_KEY }} - export_default_credentials: true - name: Gcloud Login + credentials_json: ${{ secrets.GCP_SA_KEY }} + create_credentials_file: true + env: + has_auth: ${{ secrets.GCP_PROJECT_ID && secrets.GCP_SA_KEY && 1 }} + - name: Install Trivy (latest) run: | + : "Install Trivy (latest)" TRIVY_VERSION=$(curl --silent "https://api.github.com/repos/aquasecurity/trivy/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/') echo Using Trivy v${TRIVY_VERSION} wget https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}/trivy_${TRIVY_VERSION}_Linux-64bit.deb sudo dpkg -i trivy_${TRIVY_VERSION}_Linux-64bit.deb - name: Set up Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v6 with: go-version-file: "go.mod" - name: Run tests env: - GITHUB_TOKEN: ${{ secrets.CLOUDBUILD_GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.CLOUDBUILD_GITHUB_TOKEN || github.token }} + SKIP_GCLOUD_TESTS: ${{ secrets.GCP_SA_KEY == '' && '1' || '' }} + HAS_CLOUDBUILD_GITHUB_TOKEN: ${{ secrets.CLOUDBUILD_GITHUB_TOKEN && '1' || '' }} TEST_PKG: ./... # Run all tests run: make test - - uses: testspace-com/setup-testspace@v1 + - name: Set up testspaceTestspace + uses: testspace-com/setup-testspace@v1 with: domain: solo-io.testspace.com - if: ${{ always() && github.event_name == 'push' && github.ref == 'refs/heads/master' }} + if: ${{ always() && github.event_name == 'push' && github.ref == 'refs/heads/master' && github.repository_owner == 'solo-io' }} - name: Push result to Testspace server run: | testspace push --verbose "**/junit.xml" - if: ${{ always() && github.event_name == 'push' && github.ref == 'refs/heads/master' }} + if: ${{ always() && github.event_name == 'push' && github.ref == 'refs/heads/master' && github.repository_owner == 'solo-io' }} diff --git a/Makefile b/Makefile index 6a8f6e3b..4af7686f 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,7 @@ clean: ## Clean any local assets GINKGO_VERSION ?= $(shell echo $(shell go list -m github.com/onsi/ginkgo/v2) | cut -d' ' -f2) GINKGO_ENV ?= GOLANG_PROTOBUF_REGISTRATION_CONFLICT=ignore ACK_GINKGO_DEPRECATIONS=$(GINKGO_VERSION) -GINKGO_FLAGS ?= -v -tags=purego -compilers=4 -fail-fast -race -randomize-suites -randomize-all -skip-package=./installutils/kubeinstall,./debugutils/test +GINKGO_FLAGS ?= -v -tags=purego -compilers=4 $(shell [ -z "${CI}" ] && echo '-fail-fast') -race -randomize-suites -randomize-all -skip-package=./installutils/kubeinstall,./debugutils/test GINKGO_REPORT_FLAGS ?= --json-report=test-report.json --junit-report=junit.xml -output-dir=$(OUTPUT_DIR) GINKGO_COVERAGE_FLAGS ?= --cover --covermode=atomic --coverprofile=coverage.cov TEST_PKG ?= ./... # Default to run all tests diff --git a/changelog/v0.28.7/handle-prs-from-forks.yaml b/changelog/v0.28.7/handle-prs-from-forks.yaml new file mode 100644 index 00000000..860d21ad --- /dev/null +++ b/changelog/v0.28.7/handle-prs-from-forks.yaml @@ -0,0 +1,80 @@ +changelog: + - type: DEPENDENCY_BUMP + dependencyOwner: actions + dependencyRepo: checkout + dependencyTag: v5 + description: "Use node 24" + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: DEPENDENCY_BUMP + dependencyOwner: actions + dependencyRepo: setup-go + dependencyTag: v6 + description: "Use node 24" + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: DEPENDENCY_BUMP + dependencyOwner: actions + dependencyRepo: setup-python + dependencyTag: v6 + description: "Use node 24" + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: DEPENDENCY_BUMP + dependencyOwner: styfle + dependencyRepo: cancel-workflow-action + dependencyTag: 0.12.1 + description: "Bump to node20" + resolvesIssue: false + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + dependencyOwner: google-github-actions + dependencyRepo: auth + dependencyTag: v2 + description: "Replace deprecated action flow" + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + resolvesIssue: false + description: Show all failures in CI. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + resolvesIssue: false + description: Replace deprecated methods. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + resolvesIssue: false + description: Add name to testspace step. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + resolvesIssue: false + description: Add name to testspace step. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + resolvesIssue: false + description: Add pretty comments to workflows. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + resolvesIssue: false + description: Skip gcloud steps for forks without gcloud secrets. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + resolvesIssue: false + description: Skip testspace steps for forks. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + resolvesIssue: false + description: Skip write steps for forks without cloudbuild secrets. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + resolvesIssue: false + description: Use public repositories for tests for forks without github secrets. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: NON_USER_FACING + resolvesIssue: false + description: Use public repositories for tests. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: FIX + resolvesIssue: false + description: Fix running tests for forks. + issueLink: https://github.com/solo-io/go-utils/pull/556 + - type: FIX + issueLink: https://github.com/solo-io/go-utils/pull/556 + description: Fixing handling of PRs from forks + resolvesIssue: true diff --git a/changelogutils/changelog_test.go b/changelogutils/changelog_test.go index 0f23db56..beee3c58 100644 --- a/changelogutils/changelog_test.go +++ b/changelogutils/changelog_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -415,7 +414,7 @@ closing ctx := context.Background() client, err := githubutils.GetClient(ctx) Expect(err).NotTo(HaveOccurred()) - hasChangelog, err := changelogutils.RefHasChangelog(ctx, client, "solo-io", "testrepo", "master") + hasChangelog, err := changelogutils.RefHasChangelog(ctx, client, "solo-io", "reporting-client", "master") Expect(err).NotTo(HaveOccurred()) Expect(hasChangelog).To(BeTrue()) }) @@ -508,7 +507,7 @@ func createSubdirs(dir string, names ...string) error { } func mustWriteTestDir() string { - tmpDir, err := ioutil.TempDir("", "changelog-test-") + tmpDir, err := os.MkdirTemp("", "changelog-test-") Expect(err).NotTo(HaveOccurred()) return tmpDir } diff --git a/changelogutils/reader_test.go b/changelogutils/reader_test.go index 03a0e25b..121080ff 100644 --- a/changelogutils/reader_test.go +++ b/changelogutils/reader_test.go @@ -2,6 +2,7 @@ package changelogutils_test import ( "context" + "fmt" "os" "path/filepath" "time" @@ -27,8 +28,6 @@ var _ = Describe("ReaderTest", func() { const ( owner = "solo-io" - repo = "testrepo" - sha = "9065a9a84e286ea7f067f4fc240944b0a4d4c82a" ) var ( @@ -41,8 +40,22 @@ var _ = Describe("ReaderTest", func() { file = changelogutils.ChangelogFile{ Entries: []*changelogutils.ChangelogEntry{&entry}, } + log = "1.yaml" + repo = "testrepo" + sha = "9065a9a84e286ea7f067f4fc240944b0a4d4c82a" ) + if os.Getenv("HAS_CLOUDBUILD_GITHUB_TOKEN") == "" { + log = "new-signature-manager.yaml" + repo = "reporting-client" + sha = "af5d207720ee6b548704b06bfa6631f9a2897294" + entry = changelogutils.ChangelogEntry{ + Type: changelogutils.NEW_FEATURE, + Description: "New signature manager implementation to be used in CLI clients that writes the signature to ~/.soloio", + IssueLink: "https://github.com/solo-io/gloo/issues/1559", + } + } + BeforeEach(func() { client, err := githubutils.GetClient(ctx) Expect(err).NotTo(HaveOccurred()) @@ -51,7 +64,7 @@ var _ = Describe("ReaderTest", func() { }) It("can read changelog file", func() { - changelogFile, err := reader.ReadChangelogFile(ctx, "changelog/v0.1.1/1.yaml") + changelogFile, err := reader.ReadChangelogFile(ctx, fmt.Sprintf("changelog/v0.1.1/%s", log)) Expect(err).NotTo(HaveOccurred()) Expect(*changelogFile).To(BeEquivalentTo(file)) }) diff --git a/cliutils/port_forward.go b/cliutils/port_forward.go index e4aec55f..d5246deb 100644 --- a/cliutils/port_forward.go +++ b/cliutils/port_forward.go @@ -3,7 +3,7 @@ package cliutils import ( "context" "fmt" - "io/ioutil" + "io" "net" "net/http" "os" @@ -71,7 +71,7 @@ func PortForwardGet(ctx context.Context, namespace string, resource string, loca time.Sleep(retryInterval) continue } - b, err := ioutil.ReadAll(res.Body) + b, err := io.ReadAll(res.Body) if err != nil { errs <- err time.Sleep(retryInterval) diff --git a/docker/docker_test.go b/docker/docker_test.go index 1fe7812b..f5b1f791 100644 --- a/docker/docker_test.go +++ b/docker/docker_test.go @@ -2,7 +2,7 @@ package docker_test import ( "context" - "io/ioutil" + "os" "os/exec" . "github.com/onsi/ginkgo/v2" @@ -50,14 +50,14 @@ var _ = Describe("Docker", func() { Context("Save", func() { It("can save a valid, present container", func() { pullValidImage() - file, err := ioutil.TempFile("", "docker_test") + file, err := os.CreateTemp("", "docker_test") Expect(err).NotTo(HaveOccurred()) err = docker.Save(validImage, file.Name()) Expect(err).NotTo(HaveOccurred()) }) It("cannot save an invalid container", func() { - file, err := ioutil.TempFile("", "docker_test") + file, err := os.CreateTemp("", "docker_test") Expect(err).NotTo(HaveOccurred()) err = docker.Save(invalidImage, file.Name()) Expect(err).To(HaveOccurred()) diff --git a/fileutils/messages_test.go b/fileutils/messages_test.go index 8ef8b87f..287a5423 100644 --- a/fileutils/messages_test.go +++ b/fileutils/messages_test.go @@ -4,7 +4,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "io/ioutil" "os" "github.com/gogo/protobuf/types" @@ -14,7 +13,7 @@ import ( var _ = Describe("Messages", func() { var filename string BeforeEach(func() { - f, err := ioutil.TempFile("", "messages_test") + f, err := os.CreateTemp("", "messages_test") Expect(err).NotTo(HaveOccurred()) filename = f.Name() }) @@ -32,7 +31,7 @@ var _ = Describe("Messages", func() { err := WriteToFile(filename, input) Expect(err).NotTo(HaveOccurred()) - b, err := ioutil.ReadFile(filename) + b, err := os.ReadFile(filename) Expect(err).NotTo(HaveOccurred()) Expect(string(b)).To(Equal("foo: bar\n")) diff --git a/gcloudutils/builders/gcloud_test.go b/gcloudutils/builders/gcloud_test.go index 18913de4..90ada87c 100644 --- a/gcloudutils/builders/gcloud_test.go +++ b/gcloudutils/builders/gcloud_test.go @@ -2,6 +2,7 @@ package builders import ( "context" + "os" "path" "cloud.google.com/go/storage" @@ -23,39 +24,41 @@ var _ = Describe("gcloud unit tests", func() { Expect(dir).To(Equal("gs://one/two/")) }) - Context("builders", func() { - var builderCtx *mockBuilderContext - BeforeEach(func() { - var err error - builderCtx, err = DefaultMockBuilderContext(ctx) - Expect(err).NotTo(HaveOccurred()) - }) - Context("storage source", func() { - var ( - sb *StorageBuilder - ) - + if os.Getenv("SKIP_GCLOUD_TESTS") == "" { + Context("builders", func() { + var builderCtx *mockBuilderContext BeforeEach(func() { var err error - client, err := storage.NewClient(ctx) - Expect(err).NotTo(HaveOccurred()) - - sb = &StorageBuilder{ - client: client, - } - }) - It("can init build with sha", func() { - _, err := sb.InitBuildWithSha(ctx, builderCtx) + builderCtx, err = DefaultMockBuilderContext(ctx) Expect(err).NotTo(HaveOccurred()) }) - It("can init build with tag", func() { - _, err := sb.InitBuildWithTag(ctx, builderCtx) - Expect(err).NotTo(HaveOccurred()) + Context("storage source", func() { + var ( + sb *StorageBuilder + ) + + BeforeEach(func() { + var err error + client, err := storage.NewClient(ctx) + Expect(err).NotTo(HaveOccurred()) + + sb = &StorageBuilder{ + client: client, + } + }) + It("can init build with sha", func() { + _, err := sb.InitBuildWithSha(ctx, builderCtx) + Expect(err).NotTo(HaveOccurred()) + }) + It("can init build with tag", func() { + _, err := sb.InitBuildWithTag(ctx, builderCtx) + Expect(err).NotTo(HaveOccurred()) + }) }) - }) - Context("repo source", func() { + Context("repo source", func() { + }) }) - }) + } }) diff --git a/gcloudutils/builders/storage_builder.go b/gcloudutils/builders/storage_builder.go index 41172880..42ba44b3 100644 --- a/gcloudutils/builders/storage_builder.go +++ b/gcloudutils/builders/storage_builder.go @@ -6,7 +6,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "os" "strings" @@ -104,7 +103,7 @@ func (sb *StorageBuilder) copyArchiveToStorage(ctx context.Context, builderCtx B return nil, err } - tmpf, err := ioutil.TempFile("", "*.tar.gz") + tmpf, err := os.CreateTemp("", "*.tar.gz") if err != nil { logger.Error().Err(err).Msg("can't create temp file") return nil, err @@ -169,7 +168,7 @@ func (sb *StorageBuilder) copyToBucket(ctx context.Context, builderCtx BuildCont func removeGHPrefix(ctx context.Context, archiveFile string) (*os.File, error) { logger := zerolog.Ctx(ctx) - tmpf, err := ioutil.TempFile("", "*.tar.gz") + tmpf, err := os.CreateTemp("", "*.tar.gz") if err != nil { logger.Error().Err(err).Msg("can't create temp file") return nil, err diff --git a/gcloudutils/clients.go b/gcloudutils/clients.go index 2f72768c..7ca1ddf0 100644 --- a/gcloudutils/clients.go +++ b/gcloudutils/clients.go @@ -3,7 +3,6 @@ package gcloudutils import ( "context" "fmt" - "io/ioutil" "os" "path/filepath" @@ -41,7 +40,7 @@ func credsFromProjectId(ctx context.Context, projectId string) (*google.Credenti contextutils.LoggerFrom(ctx).Debugw("Looking for creds for project", zap.String("projectId", projectId), zap.String("credsFile", pathToCredsFile)) - credByt, err := ioutil.ReadFile(pathToCredsFile) + credByt, err := os.ReadFile(pathToCredsFile) if err != nil { contextutils.LoggerFrom(ctx).Errorw("Error reading creds file", zap.Error(err), diff --git a/githubutils/githubutils_test.go b/githubutils/githubutils_test.go index 7f00b1f8..933bd75a 100644 --- a/githubutils/githubutils_test.go +++ b/githubutils/githubutils_test.go @@ -2,7 +2,6 @@ package githubutils import ( "context" - "io/ioutil" "os" "github.com/google/go-github/v32/github" @@ -16,10 +15,10 @@ var _ = Describe("github utils", func() { client *github.Client ctx = context.Background() owner = "solo-io" - reponame = "testrepo" - repoWithoutReleasesName = "testrepo-noreleases" - version = "v0.0.16" - ref = "v0.0.17" + reponame = "reporting-client" + repoWithoutReleasesName = "unik-hub" + version = "v0.1.0" + ref = "v0.1.3" ) var _ = BeforeEach(func() { @@ -72,9 +71,9 @@ var _ = Describe("github utils", func() { }) func mustSetupTempFiles() (file *os.File, dir string) { - tmpf, err := ioutil.TempFile("", "tar-file-") + tmpf, err := os.CreateTemp("", "tar-file-") Expect(err).NotTo(HaveOccurred()) - tmpd, err := ioutil.TempDir("", "tar-dir-") + tmpd, err := os.MkdirTemp("", "tar-dir-") Expect(err).NotTo(HaveOccurred()) return tmpf, tmpd } diff --git a/githubutils/repo.go b/githubutils/repo.go index c5377cfa..714075bf 100644 --- a/githubutils/repo.go +++ b/githubutils/repo.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "math" "net/http" "os" @@ -139,7 +138,7 @@ func GetRawGitFile(ctx context.Context, client *github.Client, content *github.R } defer r.Close() - byt, err := ioutil.ReadAll(r) + byt, err := io.ReadAll(r) return byt, err } diff --git a/githubutils/repo_client_test.go b/githubutils/repo_client_test.go index 74677d06..2848de1e 100644 --- a/githubutils/repo_client_test.go +++ b/githubutils/repo_client_test.go @@ -3,6 +3,7 @@ package githubutils_test import ( "context" "fmt" + "os" "strings" "github.com/solo-io/go-utils/randutils" @@ -23,10 +24,26 @@ var _ = Describe("repo client utils", func() { repo = "testrepo" repoWithoutReleasesName = "testrepo-noreleases" sha = "9065a9a84e286ea7f067f4fc240944b0a4d4c82a" + commitsInSha = 3 otherSha = "ea649cd931820a6a59970b051d480094f9d61c4e" pr = 62 + commit1 = "6d389bc860e1cefdcbc99d43979e62104f13092f" + commit2 = "9065a9a84e286ea7f067f4fc240944b0a4d4c82a" + tagWithSha = "v0.1.16" + shaForTag = "04da4a385be3fde4797963cd4f3f76a185e56ba7" ) + if os.Getenv("HAS_CLOUDBUILD_GITHUB_TOKEN") == "" { + repo = "reporting-client" + repoWithoutReleasesName = "unik-hub" + sha = "af5d207720ee6b548704b06bfa6631f9a2897294" + commitsInSha = 2 + commit1 = "7ef898bc3df32db0e1ed2dee70a838c955a7b422" + commit2 = "f47eacc21bd62e6bc8bb8954af0dc1817079af0d" + tagWithSha = "v0.1.2" + shaForTag = "a1c75ffaa40ea2b89368bfc338dc3f6f990b6df2" + } + BeforeEach(func() { c, err := githubutils.GetClient(ctx) Expect(err).To(BeNil()) @@ -66,23 +83,23 @@ var _ = Describe("repo client utils", func() { It("can do a commit comparison", func() { client = githubutils.NewRepoClient(githubClient, owner, repo) - cc, err := client.CompareCommits(ctx, "6d389bc860e1cefdcbc99d43979e62104f13092f", "9065a9a84e286ea7f067f4fc240944b0a4d4c82a") + cc, err := client.CompareCommits(ctx, commit1, commit2) Expect(err).To(BeNil()) Expect(cc.Files).To(HaveLen(5)) }) It("can get sha for tag", func() { client = githubutils.NewRepoClient(githubClient, owner, repo) - sha, err := client.GetShaForTag(ctx, "v0.1.16") + sha, err := client.GetShaForTag(ctx, tagWithSha) Expect(err).To(BeNil()) - Expect(sha).To(Equal("04da4a385be3fde4797963cd4f3f76a185e56ba7")) + Expect(sha).To(Equal(shaForTag)) }) It("can get a commit", func() { client = githubutils.NewRepoClient(githubClient, owner, repo) commit, err := client.GetCommit(ctx, sha) Expect(err).To(BeNil()) - Expect(len(commit.Files)).To(Equal(3)) + Expect(len(commit.Files)).To(Equal(commitsInSha)) }) expectStatus := func(actual, expected *github.RepoStatus) { @@ -100,40 +117,48 @@ var _ = Describe("repo client utils", func() { expectStatus(loaded, status) } - It("can manage status", func() { - client = githubutils.NewRepoClient(githubClient, owner, repo) - // randomizing this would create a (slim) potential race - // not randomizing makes it less easy to validate that the create worked, but we'll assume github api responses are accurate - status := &github.RepoStatus{ - State: github.String(githubutils.STATUS_SUCCESS), - Context: github.String("test"), - Description: github.String("test"), // longer than 140 characters will be truncated - } - testManageStatus(client, status, sha) - }) + Context("With write token", func() { + BeforeEach(func() { + if os.Getenv("HAS_CLOUDBUILD_GITHUB_TOKEN") == "" { + Skip("Needs write token") + } + }) - It("can manage status even when it exceeds 140 character", func() { - client = githubutils.NewRepoClient(githubClient, owner, repo) - // randomizing this would create a (slim) potential race - // not randomizing makes it less easy to validate that the create worked, but we'll assume github api responses are accurate - status := &github.RepoStatus{ - State: github.String(githubutils.STATUS_SUCCESS), - Context: github.String("test"), - Description: github.String(strings.Repeat("test", 40)), // longer than 140 characters will be truncated - } - testManageStatus(client, status, otherSha) // don't share sha with other test to avoid race - }) + It("can manage status", func() { + client = githubutils.NewRepoClient(githubClient, owner, repo) + // randomizing this would create a (slim) potential race + // not randomizing makes it less easy to validate that the create worked, but we'll assume github api responses are accurate + status := &github.RepoStatus{ + State: github.String(githubutils.STATUS_SUCCESS), + Context: github.String("test"), + Description: github.String("test"), // longer than 140 characters will be truncated + } + testManageStatus(client, status, sha) + }) - It("can create and delete comments", func() { - client = githubutils.NewRepoClient(githubClient, owner, repo) - body := fmt.Sprintf("test-%s", randutils.RandString(4)) - comment := &github.IssueComment{ - Body: github.String(body), - } - stored, err := client.CreateComment(ctx, pr, comment) - Expect(err).To(BeNil()) - err = client.DeleteComment(ctx, stored.GetID()) - Expect(err).To(BeNil()) + It("can manage status even when it exceeds 140 character", func() { + client = githubutils.NewRepoClient(githubClient, owner, repo) + // randomizing this would create a (slim) potential race + // not randomizing makes it less easy to validate that the create worked, but we'll assume github api responses are accurate + status := &github.RepoStatus{ + State: github.String(githubutils.STATUS_SUCCESS), + Context: github.String("test"), + Description: github.String(strings.Repeat("test", 40)), // longer than 140 characters will be truncated + } + testManageStatus(client, status, otherSha) // don't share sha with other test to avoid race + }) + + It("can create and delete comments", func() { + client = githubutils.NewRepoClient(githubClient, owner, repo) + body := fmt.Sprintf("test-%s", randutils.RandString(4)) + comment := &github.IssueComment{ + Body: github.String(body), + } + stored, err := client.CreateComment(ctx, pr, comment) + Expect(err).To(BeNil()) + err = client.DeleteComment(ctx, stored.GetID()) + Expect(err).To(BeNil()) + }) }) Context("Can properly find the most recent tag before an SHA", func() { diff --git a/githubutils/upload_release_asset.go b/githubutils/upload_release_asset.go index cde0ae53..f0e4dbb2 100644 --- a/githubutils/upload_release_asset.go +++ b/githubutils/upload_release_asset.go @@ -5,7 +5,6 @@ import ( "crypto/sha256" "encoding/hex" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -121,7 +120,7 @@ func writeSha256OrExit(ctx context.Context, file *os.File, outputPath string) { contextutils.LoggerFrom(ctx).Fatal(err) } sha256String := hex.EncodeToString(h.Sum(nil)) + " " + filepath.Base(file.Name()) + "\n" - err := ioutil.WriteFile(outputPath, []byte(sha256String), 0700) + err := os.WriteFile(outputPath, []byte(sha256String), 0700) if err != nil { contextutils.LoggerFrom(ctx).Fatal(err) } diff --git a/osutils/client.go b/osutils/client.go index b307dadd..2cbbd7b9 100644 --- a/osutils/client.go +++ b/osutils/client.go @@ -1,7 +1,6 @@ package osutils import ( - "io/ioutil" "os" ) @@ -18,7 +17,7 @@ func (*osClient) Getenv(key string) string { } func (*osClient) ReadFile(path string) ([]byte, error) { - return ioutil.ReadFile(path) + return os.ReadFile(path) } func NewOsClient() OsClient { diff --git a/pkgmgmtutils/internal/local_clone_change_pusher.go b/pkgmgmtutils/internal/local_clone_change_pusher.go index 2a63eeab..fc23d2fb 100644 --- a/pkgmgmtutils/internal/local_clone_change_pusher.go +++ b/pkgmgmtutils/internal/local_clone_change_pusher.go @@ -2,7 +2,6 @@ package internal import ( "context" - "io/ioutil" "os" "path/filepath" "time" @@ -33,7 +32,7 @@ func (l *localCloneChangePusher) UpdateAndPush( formulaOptions *formula_updater_types.FormulaOptions, ) error { // create temp dir for local git clone - dirTemp, err := ioutil.TempDir("", formulaOptions.RepoName) + dirTemp, err := os.MkdirTemp("", formulaOptions.RepoName) if err != nil { return err } @@ -86,7 +85,7 @@ func (l *localCloneChangePusher) UpdateAndPush( formulaPath := filepath.Join(dirTemp, formulaOptions.Path) - byt, err := ioutil.ReadFile(formulaPath) + byt, err := os.ReadFile(formulaPath) if err != nil { return err } @@ -98,7 +97,7 @@ func (l *localCloneChangePusher) UpdateAndPush( } // Write Updated file to git clone directory - err = ioutil.WriteFile(formulaPath, byt, 0644) + err = os.WriteFile(formulaPath, byt, 0644) if err != nil { return err } diff --git a/pkgmgmtutils/internal/remote_sha_getter.go b/pkgmgmtutils/internal/remote_sha_getter.go index 66ed33cf..110ef03f 100644 --- a/pkgmgmtutils/internal/remote_sha_getter.go +++ b/pkgmgmtutils/internal/remote_sha_getter.go @@ -2,7 +2,7 @@ package internal import ( "fmt" - "io/ioutil" + "io" "net/http" "strings" @@ -25,7 +25,7 @@ func (*remoteShaGetter) GetShaFromUrl(url string) (sha string, err error) { } defer resp.Body.Close() - b, err := ioutil.ReadAll(resp.Body) + b, err := io.ReadAll(resp.Body) if err != nil { return "", err } diff --git a/securityscanutils/commands/format_results.go b/securityscanutils/commands/format_results.go index 36cc5c70..48692b96 100644 --- a/securityscanutils/commands/format_results.go +++ b/securityscanutils/commands/format_results.go @@ -5,7 +5,7 @@ import ( "context" "encoding/gob" "fmt" - "io/ioutil" + "io" "log" "net/http" "os" @@ -208,7 +208,7 @@ func GetSecurityScanReport(url string) (string, error) { var report string if resp.StatusCode == http.StatusOK { - bodyBytes, _ := ioutil.ReadAll(resp.Body) + bodyBytes, _ := io.ReadAll(resp.Body) report = string(bodyBytes) } else if resp.StatusCode == http.StatusNotFound { // Older releases may be missing scan results diff --git a/securityscanutils/securityscan_test.go b/securityscanutils/securityscan_test.go index b91bc944..c3abf061 100644 --- a/securityscanutils/securityscan_test.go +++ b/securityscanutils/securityscan_test.go @@ -3,7 +3,6 @@ package securityscanutils_test import ( "context" "fmt" - "io/ioutil" "os" "path" "sort" @@ -29,7 +28,7 @@ var _ = Describe("Security Scan Suite", func() { BeforeEach(func() { var err error - outputDir, err = ioutil.TempDir("", "") + outputDir, err = os.MkdirTemp("", "") Expect(err).NotTo(HaveOccurred()) }) @@ -216,7 +215,7 @@ var _ = Describe("Security Scan Suite", func() { // Accepts a list of file names and passes all tests only if the directory path passed in // as dir includes all fileNames passed in. func ExpectDirToHaveFiles(dir string, fileNames ...string) { - dirResults, err := ioutil.ReadDir(dir) + dirResults, err := os.ReadDir(dir) ExpectWithOffset(1, err).NotTo(HaveOccurred()) ExpectWithOffset(1, dirResults).To(HaveLen(len(fileNames))) var dirFiles []string diff --git a/securityscanutils/trivy_scanner_test.go b/securityscanutils/trivy_scanner_test.go index 5af9c5d8..bb666a39 100644 --- a/securityscanutils/trivy_scanner_test.go +++ b/securityscanutils/trivy_scanner_test.go @@ -2,7 +2,6 @@ package securityscanutils_test import ( "context" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -28,7 +27,7 @@ var _ = Describe("Trivy Scanner", func() { t = NewTrivyScanner(executils.CombinedOutputWithStatus) inputMarkdownTemplateFile, err = GetTemplateFile(MarkdownTrivyTemplate) Expect(err).NotTo(HaveOccurred()) - outputDir, err := ioutil.TempDir("", "") + outputDir, err := os.MkdirTemp("", "") Expect(err).NotTo(HaveOccurred()) outputFile = filepath.Join(outputDir, "test_report.docgen") inputImage = "quay.io/solo-io/gloo:1.11.1" diff --git a/securityscanutils/trivy_templates.go b/securityscanutils/trivy_templates.go index c9611772..db53e11c 100644 --- a/securityscanutils/trivy_templates.go +++ b/securityscanutils/trivy_templates.go @@ -1,7 +1,7 @@ package securityscanutils import ( - "io/ioutil" + "os" "github.com/rotisserie/eris" ) @@ -30,7 +30,7 @@ Trivy Returned Empty Report // Create tempoarary file that contains the trivy template // Trivy CLI only accepts files as input for a template, so this is a workaround func GetTemplateFile(trivyTemplate string) (string, error) { - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") if err != nil { return "", eris.Wrap(err, "Unable to create temporary file to write template to") } diff --git a/test/checks/checks_test.go b/test/checks/checks_test.go index 8445004d..a5acfd57 100644 --- a/test/checks/checks_test.go +++ b/test/checks/checks_test.go @@ -1,7 +1,7 @@ package checks import ( - "io/ioutil" + "os" "os/exec" "strings" @@ -19,7 +19,7 @@ var _ = Describe("Checks", func() { gomod, err := exec.Command("go", "env", "GOMOD").CombinedOutput() Expect(err).NotTo(HaveOccurred()) gomodfile := strings.TrimSpace(string(gomod)) - data, err := ioutil.ReadFile(gomodfile) + data, err := os.ReadFile(gomodfile) Expect(err).NotTo(HaveOccurred()) modFile, err := modfile.Parse(gomodfile, data, nil) diff --git a/testutils/consul.go b/testutils/consul.go index 6703a9b2..f6df6641 100644 --- a/testutils/consul.go +++ b/testutils/consul.go @@ -6,8 +6,6 @@ import ( "os/exec" "path/filepath" - "io/ioutil" - "time" "github.com/onsi/ginkgo/v2" @@ -30,7 +28,7 @@ func NewConsulFactory() (*ConsulFactory, error) { } // try to grab one form docker... - tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "consul") + tmpdir, err := os.MkdirTemp(os.Getenv("HELPER_TMP"), "consul") if err != nil { return nil, err } @@ -48,7 +46,7 @@ docker rm -f $CID `, defaultConsulDockerImage, defaultConsulDockerImage) scriptfile := filepath.Join(tmpdir, "getconsul.sh") - ioutil.WriteFile(scriptfile, []byte(bash), 0755) + os.WriteFile(scriptfile, []byte(bash), 0755) cmd := exec.Command("bash", scriptfile) cmd.Dir = tmpdir @@ -83,7 +81,7 @@ type ConsulInstance struct { func (ef *ConsulFactory) NewConsulInstance() (*ConsulInstance, error) { // try to grab one form docker... - tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "consul") + tmpdir, err := os.MkdirTemp(os.Getenv("HELPER_TMP"), "consul") if err != nil { return nil, err } diff --git a/testutils/fail_handler_unix.go b/testutils/fail_handler_unix.go index 69d1f09a..18faad2d 100644 --- a/testutils/fail_handler_unix.go +++ b/testutils/fail_handler_unix.go @@ -5,7 +5,6 @@ package testutils import ( "fmt" - "io/ioutil" "os" "os/signal" "runtime/debug" @@ -34,7 +33,7 @@ func init() { } IsDebuggerPresent = func() bool { - f, err := ioutil.ReadFile("/proc/self/status") + f, err := os.ReadFile("/proc/self/status") if err != nil { // no status so we don't know return false diff --git a/testutils/runners/consul/consul.go b/testutils/runners/consul/consul.go index a9fe1234..6e8ca1f4 100644 --- a/testutils/runners/consul/consul.go +++ b/testutils/runners/consul/consul.go @@ -2,7 +2,6 @@ package consul import ( "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -42,7 +41,7 @@ func NewConsulFactory() (*ConsulFactory, error) { } // try to grab one form docker... - tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "consul") + tmpdir, err := os.MkdirTemp(os.Getenv("HELPER_TMP"), "consul") if err != nil { return nil, err } @@ -60,7 +59,7 @@ docker rm -f $CID `, defaultConsulDockerImage, defaultConsulDockerImage) scriptfile := filepath.Join(tmpdir, "getconsul.sh") - ioutil.WriteFile(scriptfile, []byte(bash), 0755) + os.WriteFile(scriptfile, []byte(bash), 0755) cmd := exec.Command("bash", scriptfile) cmd.Dir = tmpdir @@ -97,7 +96,7 @@ type ConsulInstance struct { func (ef *ConsulFactory) NewConsulInstance() (*ConsulInstance, error) { // try to grab one form docker... - tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "consul") + tmpdir, err := os.MkdirTemp(os.Getenv("HELPER_TMP"), "consul") if err != nil { return nil, err } diff --git a/testutils/vault.go b/testutils/vault.go index 7bfb665f..5228c573 100644 --- a/testutils/vault.go +++ b/testutils/vault.go @@ -6,8 +6,6 @@ import ( "os/exec" "path/filepath" - "io/ioutil" - "time" "bytes" @@ -36,7 +34,7 @@ func NewVaultFactory() (*VaultFactory, error) { } // try to grab one from docker... - tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "vault") + tmpdir, err := os.MkdirTemp(os.Getenv("HELPER_TMP"), "vault") if err != nil { return nil, err } @@ -54,7 +52,7 @@ docker rm -f $CID `, defaultVaultDockerImage, defaultVaultDockerImage) scriptfile := filepath.Join(tmpdir, "getvault.sh") - ioutil.WriteFile(scriptfile, []byte(bash), 0755) + os.WriteFile(scriptfile, []byte(bash), 0755) cmd := exec.Command("bash", scriptfile) cmd.Dir = tmpdir @@ -90,7 +88,7 @@ type VaultInstance struct { func (ef *VaultFactory) NewVaultInstance() (*VaultInstance, error) { // try to grab one form docker... - tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "vault") + tmpdir, err := os.MkdirTemp(os.Getenv("HELPER_TMP"), "vault") if err != nil { return nil, err } diff --git a/versionutils/repo_test.go b/versionutils/repo_test.go index 937ca6d9..3119d5fd 100644 --- a/versionutils/repo_test.go +++ b/versionutils/repo_test.go @@ -1,7 +1,6 @@ package versionutils_test import ( - "io/ioutil" "os" "path/filepath" @@ -36,10 +35,10 @@ var _ = Describe("Repo", func() { ) BeforeEach(func() { - tmpDir, err = ioutil.TempDir("", "toml-parse-test-") + tmpDir, err = os.MkdirTemp("", "toml-parse-test-") Expect(err).NotTo(HaveOccurred()) tomlFile = filepath.Join(tmpDir, "Gopkg.toml") - err = ioutil.WriteFile(tomlFile, []byte(tomlContent), 0700) + err = os.WriteFile(tomlFile, []byte(tomlContent), 0700) Expect(err).NotTo(HaveOccurred()) tomlWrapper, err = ParseFullTomlFromDir(tmpDir) diff --git a/vfsutils/mount_test.go b/vfsutils/mount_test.go index e275d214..785bfc49 100644 --- a/vfsutils/mount_test.go +++ b/vfsutils/mount_test.go @@ -2,6 +2,7 @@ package vfsutils_test import ( "context" + "os" "github.com/solo-io/go-utils/githubutils" "github.com/solo-io/go-utils/vfsutils" @@ -14,15 +15,26 @@ var _ = Describe("mounted repo utils", func() { const ( owner = "solo-io" - repo = "testrepo" - sha = "9065a9a84e286ea7f067f4fc240944b0a4d4c82a" ) var ( - ctx = context.Background() - mountedRepo vfsutils.MountedRepo + ctx = context.Background() + mountedRepo vfsutils.MountedRepo + repo = "testrepo" + sha = "9065a9a84e286ea7f067f4fc240944b0a4d4c82a" + file = "tmp.txt" + expectedContent = "another" + path = "namespace" ) + if os.Getenv("HAS_CLOUDBUILD_GITHUB_TOKEN") == "" { + repo = "unik" + sha = "767fb7285ea9c893efcced90a612c4e253ef8e4b" + file = "README.md" + expectedContent = "UniK" + path = "containers/utils/vsphere-client/src/main/java/com/emc/unik" + } + BeforeEach(func() { client, err := githubutils.GetClient(ctx) Expect(err).NotTo(HaveOccurred()) @@ -30,13 +42,13 @@ var _ = Describe("mounted repo utils", func() { }) It("can get contents", func() { - contents, err := mountedRepo.GetFileContents(ctx, "tmp.txt") + contents, err := mountedRepo.GetFileContents(ctx, file) Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(ContainSubstring("another")) + Expect(string(contents)).To(ContainSubstring(expectedContent)) }) It("can list files", func() { - files, err := mountedRepo.ListFiles(ctx, "namespace") + files, err := mountedRepo.ListFiles(ctx, path) Expect(err).NotTo(HaveOccurred()) Expect(len(files)).To(Equal(1)) })