github.com/metasources/buildx@v0.0.0-20230418141019-7aa1459cedea/test/cli/power_user_cmd_test.go (about) 1 package cli 2 3 import ( 4 "testing" 5 ) 6 7 func TestPowerUserCmdFlags(t *testing.T) { 8 secretsFixture := getFixtureImage(t, "image-secrets") 9 tests := []struct { 10 name string 11 args []string 12 env map[string]string 13 assertions []traitAssertion 14 }{ 15 { 16 name: "no-args-shows-help", 17 args: []string{"power-user"}, 18 assertions: []traitAssertion{ 19 assertInOutput("an image/directory argument is required"), // specific error that should be shown 20 assertInOutput("Run bulk operations on container images"), // excerpt from help description 21 assertFailingReturnCode, 22 }, 23 }, 24 { 25 name: "default-results-w-pkg-coverage", 26 args: []string{"power-user", "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")}, 27 assertions: []traitAssertion{ 28 assertNotInOutput(" command is deprecated"), // only the root command should be deprecated 29 assertInOutput(`"type": "RegularFile"`), // proof of file-metadata data 30 assertInOutput(`"algorithm": "sha256"`), // proof of file-metadata default digest algorithm of sha256 31 assertInOutput(`"metadataType": "ApkMetadata"`), // proof of package artifacts data 32 assertSuccessfulReturnCode, 33 }, 34 }, 35 { 36 name: "content-cataloger-wired-up", 37 args: []string{"power-user", "docker-archive:" + secretsFixture}, 38 env: map[string]string{ 39 "BUILDX_FILE_CONTENTS_GLOBS": "/api-key.txt", 40 }, 41 assertions: []traitAssertion{ 42 assertInOutput(`"contents": "c29tZV9BcEkta0V5ID0gIjEyMzQ1QTdhOTAxYjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MCIK"`), // proof of the content cataloger 43 assertSuccessfulReturnCode, 44 }, 45 }, 46 { 47 name: "default-dir-results-w-pkg-coverage", 48 args: []string{"power-user", "dir:test-fixtures/image-pkg-coverage"}, 49 assertions: []traitAssertion{ 50 assertNotInOutput(" command is deprecated"), // only the root command should be deprecated 51 assertInOutput(`"type": "RegularFile"`), // proof of file-metadata data 52 assertInOutput(`"algorithm": "sha256"`), // proof of file-metadata default digest algorithm of sha256 53 assertInOutput(`"metadataType": "ApkMetadata"`), // proof of package artifacts data 54 assertSuccessfulReturnCode, 55 }, 56 }, 57 { 58 name: "default-secrets-results-w-reveal-values", 59 env: map[string]string{ 60 "BUILDX_SECRETS_REVEAL_VALUES": "true", 61 }, 62 args: []string{"power-user", "docker-archive:" + secretsFixture}, 63 assertions: []traitAssertion{ 64 assertInOutput(`"classification": "generic-api-key"`), // proof of the secrets cataloger finding something 65 assertInOutput(`"12345A7a901b345678901234567890123456789012345678901234567890"`), // proof of the secrets cataloger finding the api key 66 assertSuccessfulReturnCode, 67 }, 68 }, 69 { 70 name: "default-secret-results-dont-reveal-values", 71 args: []string{"power-user", "docker-archive:" + secretsFixture}, 72 assertions: []traitAssertion{ 73 assertInOutput(`"classification": "generic-api-key"`), // proof of the secrets cataloger finding something 74 assertNotInOutput(`"12345A7a901b345678901234567890123456789012345678901234567890"`), // proof of the secrets cataloger finding the api key 75 assertSuccessfulReturnCode, 76 }, 77 }, 78 { 79 name: "default-secrets-dir-results-w-reveal-values", 80 env: map[string]string{ 81 "BUILDX_SECRETS_REVEAL_VALUES": "true", 82 }, 83 args: []string{"power-user", "dir:test-fixtures/image-secrets-dir"}, 84 assertions: []traitAssertion{ 85 assertInOutput(`"classification": "generic-api-key"`), // proof of the secrets cataloger finding something 86 assertInOutput(`"12345A7a901b345678901234567890123456789012345678901234567890"`), // proof of the secrets cataloger finding the api key 87 assertSuccessfulReturnCode, 88 }, 89 }, 90 } 91 92 for _, test := range tests { 93 t.Run(test.name, func(t *testing.T) { 94 cmd, stdout, stderr := runBuildxSafe(t, test.env, test.args...) 95 for _, traitFn := range test.assertions { 96 traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode()) 97 } 98 logOutputOnFailure(t, cmd, stdout, stderr) 99 }) 100 } 101 }