github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/packager/cnab_test.go (about) 1 package packager 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "regexp" 7 "testing" 8 9 "github.com/docker/app/internal" 10 11 "github.com/docker/app/types" 12 "gotest.tools/assert" 13 "gotest.tools/golden" 14 ) 15 16 func TestToCNAB(t *testing.T) { 17 app, err := types.NewAppFromDefaultFiles("testdata/packages/packing.dockerapp") 18 assert.NilError(t, err) 19 actual, err := ToCNAB(app, "test-image") 20 assert.NilError(t, err) 21 actualJSON, err := json.MarshalIndent(actual, "", " ") 22 assert.NilError(t, err) 23 s := golden.Get(t, "bundle-json.golden") 24 expectedLiteral := regexp.QuoteMeta(string(s)) 25 expected := fmt.Sprintf(expectedLiteral, DockerAppPayloadVersionCurrent, internal.Version) 26 matches, err := regexp.Match(expected, actualJSON) 27 assert.NilError(t, err) 28 assert.Assert(t, matches) 29 } 30 31 func TestCNABBundleAPIv1(t *testing.T) { 32 const ( 33 ns = "com.docker.app." 34 cnabNs = "io.cnab." 35 ) 36 //check v1 actions are supported 37 requiredActions := []string{ 38 ns + "inspect", 39 ns + "render", 40 cnabNs + "status", 41 } 42 requiredCredentials := []string{ 43 ns + "registry-creds", 44 ns + "context", 45 } 46 requiredParameters := []string{ 47 ns + "args", 48 ns + "inspect-format", 49 ns + "kubernetes-namespace", 50 ns + "orchestrator", 51 ns + "render-format", 52 ns + "share-registry-creds", 53 } 54 55 app, err := types.NewAppFromDefaultFiles("testdata/packages/packing.dockerapp") 56 assert.NilError(t, err) 57 actual, err := ToCNAB(app, "test-image") 58 assert.NilError(t, err) 59 actualJSON, err := json.MarshalIndent(actual, "", " ") 60 assert.NilError(t, err) 61 62 var expectedJSON map[string]interface{} 63 err = json.Unmarshal(actualJSON, &expectedJSON) 64 assert.NilError(t, err) 65 66 actions := extract(expectedJSON, "actions") 67 for _, action := range requiredActions { 68 assert.Equal(t, contains(actions, action), true, fmt.Sprintf("%s not found", action)) 69 } 70 71 credentials := extract(expectedJSON, "credentials") 72 for _, cred := range requiredCredentials { 73 assert.Equal(t, contains(credentials, cred), true, fmt.Sprintf("%s not found", cred)) 74 } 75 76 params := extract(expectedJSON, "parameters") 77 for _, param := range requiredParameters { 78 assert.Equal(t, contains(params, param), true, fmt.Sprintf("%s not found", param)) 79 } 80 } 81 82 func extract(data map[string]interface{}, field string) []string { 83 var keys []string 84 for key := range data[field].(map[string]interface{}) { 85 keys = append(keys, key) 86 } 87 return keys 88 } 89 90 func contains(keyList []string, key string) bool { 91 for _, k := range keyList { 92 if key == k { 93 return true 94 } 95 } 96 return false 97 }