github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/pkg/aci/acitest/manifest.go (about) 1 // Copyright 2017 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package acitest provides utilities for ACI testing. 16 package acitest 17 18 import ( 19 "encoding/json" 20 21 "github.com/appc/spec/schema" 22 "github.com/appc/spec/schema/types" 23 ) 24 25 var ( 26 // zeroVersion specifies an empty instance of the semantic version 27 // used to compare it in the image manifest in order to detect an 28 // unspecified manifest version. 29 zeroVersion types.SemVer 30 ) 31 32 const ( 33 // defaultName defines the name of the image manifest that will be 34 // used when name was not specified. 35 defaultName = "example.com/test01" 36 ) 37 38 type ( 39 // These types are aliases to the types used in the application 40 // container definition. They used to bypass validations of the 41 // image manifest during serialization to JSON string. 42 aciApp types.App 43 aciAnnotations types.Annotations 44 aciDependencies types.Dependencies 45 aciKind types.ACKind 46 aciLabels types.Labels 47 aciName types.ACIdentifier 48 49 // aciManifest completely copies and underlying representation 50 // of the application image manifest. It wraps each attribute of 51 // the manifest into the alias in order to bypass validations. 52 aciManifest struct { 53 ACKind aciKind `json:"acKind"` 54 ACVersion types.SemVer `json:"acVersion"` 55 Name aciName `json:"name"` 56 Labels aciLabels `json:"labels,omitempty"` 57 App *aciApp `json:"app,omitempty"` 58 Annotations aciAnnotations `json:"annotations,omitempty"` 59 Dependencies aciDependencies `json:"dependencies,omitempty"` 60 PathWhitelist []string `json:"pathWhitelist,omitempty"` 61 } 62 ) 63 64 // toImageManifest converts the specified application image manifest 65 // into the non-checkable image manifest, thus make it possible to 66 // create a manifest with mistakes. 67 func toImageManifest(m *schema.ImageManifest) *aciManifest { 68 return &aciManifest{ 69 ACKind: aciKind(m.ACKind), 70 ACVersion: m.ACVersion, 71 Name: aciName(m.Name), 72 Labels: aciLabels(m.Labels), 73 App: (*aciApp)(m.App), 74 Annotations: aciAnnotations(m.Annotations), 75 Dependencies: aciDependencies(m.Dependencies), 76 PathWhitelist: m.PathWhitelist, 77 } 78 } 79 80 // ImageManifestString sets the default attributes to the specified image 81 // manifest and returns its JSON string representation. 82 func ImageManifestString(im *schema.ImageManifest) (string, error) { 83 // When nil have been passed as an argument, it will create an 84 // empty manifest and define the minimal attributes. 85 if im == nil { 86 im = new(schema.ImageManifest) 87 } 88 89 // Set the default kind of the AC image manifest. 90 if im.ACKind == "" { 91 im.ACKind = schema.ImageManifestKind 92 } 93 94 // Set the default version of the AC image manifest. 95 if im.ACVersion == zeroVersion { 96 im.ACVersion = schema.AppContainerVersion 97 } 98 99 // Set the default name of the AC image manifest. 100 if im.Name == "" { 101 im.Name = defaultName 102 } 103 104 b, err := json.Marshal(toImageManifest(im)) 105 return string(b), err 106 }