github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/define/image/v1/sealer_image_test.go (about)

     1  // Copyright © 2022 Alibaba Group Holding Ltd.
     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 v1
    16  
    17  import (
    18  	"encoding/json"
    19  	"reflect"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/sealerio/sealer/pkg/define/application"
    24  	v1 "github.com/sealerio/sealer/pkg/define/application/v1"
    25  	"github.com/sealerio/sealer/pkg/define/application/version"
    26  
    27  	"github.com/opencontainers/go-digest"
    28  	"github.com/opencontainers/image-spec/specs-go"
    29  	ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
    30  )
    31  
    32  func TestNewImageSpec(t *testing.T) {
    33  	type args struct {
    34  		spec *ImageSpec
    35  	}
    36  	tests := []struct {
    37  		name string
    38  		args args
    39  		want string
    40  	}{
    41  		{
    42  			name: "",
    43  			args: args{
    44  				spec: &ImageSpec{
    45  					ID:     "783d1c3814b9cb28dafd7c3ca34b734d5798a61dc523c0d30349636e7cd56cc6",
    46  					Name:   "cloud-image-registry.cn-shanghai.cr.aliyuncs.com/applications/1137465199671599:cnstack-acos-0.0.5-beta-07b8866-70f202",
    47  					Digest: "sha256:ebdd1badbb67f0f3de27317252618759db11782928bd206e9b155d5cc81f2076",
    48  					ManifestV1: ociv1.Manifest{
    49  						Versioned: specs.Versioned{
    50  							SchemaVersion: 2,
    51  						},
    52  						MediaType: "application/vnd.oci.image.manifest.v1+json",
    53  						Config: ociv1.Descriptor{
    54  							MediaType:   "application/vnd.oci.image.config.v1+json",
    55  							Digest:      "sha256:783d1c3814b9cb28dafd7c3ca34b734d5798a61dc523c0d30349636e7cd56cc6",
    56  							Size:        1288,
    57  							URLs:        nil,
    58  							Annotations: nil,
    59  							Platform: &ociv1.Platform{
    60  								Architecture: "",
    61  								OS:           "",
    62  								OSVersion:    "",
    63  								OSFeatures:   nil,
    64  								Variant:      "",
    65  							},
    66  						},
    67  						Layers: []ociv1.Descriptor{
    68  							{
    69  								MediaType:   "application/vnd.oci.image.layer.v1.tar+gzip",
    70  								Digest:      "sha256:4e36d2017e0a3b3e6642471bbcefc8290fc92a173bd3a5e70d9285fe4af6cb51",
    71  								Size:        3077,
    72  								URLs:        nil,
    73  								Annotations: nil,
    74  								Platform: &ociv1.Platform{
    75  									Architecture: "",
    76  									OS:           "",
    77  									OSVersion:    "",
    78  									OSFeatures:   nil,
    79  									Variant:      "",
    80  								},
    81  							},
    82  							{
    83  								MediaType:   "application/vnd.oci.image.layer.v1.tar+gzip",
    84  								Digest:      "sha256:4086b218e01c8713ed47e93f8fd24c2e714a609ff3b5268c91231dd5590e58e4",
    85  								Size:        288,
    86  								URLs:        nil,
    87  								Annotations: nil,
    88  								Platform: &ociv1.Platform{
    89  									Architecture: "",
    90  									OS:           "",
    91  									OSVersion:    "",
    92  									OSFeatures:   nil,
    93  									Variant:      "",
    94  								},
    95  							},
    96  						},
    97  						Annotations: map[string]string{
    98  							"org.opencontainers.image.base.digest": "sha256:d3801a216e40f5bc517b7e8aa1da41c31127f819f38a49356ffbd23a3d476017",
    99  							"sealer.image.extension":               "{\\\"type\\\":\\\"app-installer\\\",\\\"applications\\\":[{\\\"name\\\":\\\"cnstack-acos-0.0.5-beta\\\",\\\"type\\\":\\\"shell\\\",\\\"launchfiles\\\":[\\\"cnstack-acos-0.0.5-beta-install.sh\\\"],\\\"version\\\":\\\"v1\\\"}],\\\"launch\\\":{\\\"cmds\\\":[\\\"bash application/apps/cnstack-acos-0.0.5-beta/cnstack-acos-0.0.5-beta-install.sh\\\"]}}",
   100  						},
   101  					},
   102  					OCIv1: ociv1.Image{
   103  						Created:      wrapTimePoint(time.Now()),
   104  						Author:       "",
   105  						Architecture: "amd64",
   106  						Variant:      "",
   107  						OS:           "linux",
   108  						OSVersion:    "",
   109  						OSFeatures:   nil,
   110  						Config: ociv1.ImageConfig{
   111  							User:         "",
   112  							ExposedPorts: nil,
   113  							Env: []string{
   114  								"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
   115  							},
   116  							Entrypoint: nil,
   117  							Cmd:        nil,
   118  							Volumes:    nil,
   119  							WorkingDir: "",
   120  							Labels: map[string]string{
   121  								"io.buildah.version": "1.27.1",
   122  								"io.sealer.version":  "0.9",
   123  							},
   124  							StopSignal: "",
   125  						},
   126  						RootFS: ociv1.RootFS{
   127  							Type: "layers",
   128  							DiffIDs: []digest.Digest{
   129  								"sha256:aa735d9c504ee5a122bb323c0147a1ad317bd0e72b5eef1094025e793f6f4912",
   130  								"sha256:7c5cf4cbab5b83e603116d3b3f84287df4f8e70c3c35e3cd6d384b451bb8c26d",
   131  								"sha256:e0af0977fcb45ba29be4ef74ec1dcdbb625e01b78905bb089b8cc6b0ffc717ba",
   132  							},
   133  						},
   134  						History: []ociv1.History{
   135  							{
   136  								Created:   wrapTimePoint(time.Now()),
   137  								CreatedBy: "/bin/sh -c #(nop) COPY dir:2127a0f14734aff8fb3128a35824dcbb01dd2d0f8295e55e3ea0ab299daef971 in applications ",
   138  							},
   139  							{
   140  								Created:   wrapTimePoint(time.Now()),
   141  								CreatedBy: "/bin/sh -c #(nop) COPY file:b48c5033638953a52b8c36a41734d2e862787520a3b372e4ef1161d491c7221f in application/apps/cnstack-acos-0.0.5-beta/ ",
   142  								Comment:   "FROM c6dfe3236bb0",
   143  							},
   144  							{
   145  								Created:    wrapTimePoint(time.Now()),
   146  								CreatedBy:  "/bin/sh -c #(nop) LABEL \"io.sealer.version\"=\"0.9\"",
   147  								Comment:    "FROM 4a18afda8bf5",
   148  								EmptyLayer: true,
   149  							},
   150  							{
   151  								Created:   wrapTimePoint(time.Now()),
   152  								CreatedBy: "/bin/sh",
   153  								Comment:   "FROM cloud-image-registry.cn-shanghai.cr.aliyuncs.com/applications/1137465199671599:cnstack-acos-0.0.5-beta-07b8866-70f202170cbcb3",
   154  							},
   155  						},
   156  					},
   157  					ImageExtension: ImageExtension{
   158  						BuildClient: BuildClient{
   159  							SealerVersion:  "0.9",
   160  							BuildahVersion: "0.0.1",
   161  						},
   162  						SchemaVersion: "0.1",
   163  						Type:          "app-image",
   164  						Applications: []version.VersionedApplication{
   165  							&v1.Application{
   166  								NameVar: "app1",
   167  								TypeVar: application.ShellApp,
   168  								FilesVar: []string{
   169  									"cnstack-acos-0.0.5-beta-install.sh",
   170  								},
   171  								VersionVar: "v1",
   172  							},
   173  							&v1.Application{
   174  								NameVar: "app2",
   175  								TypeVar: application.HelmApp,
   176  								FilesVar: []string{
   177  									"chart.tgz",
   178  								},
   179  								VersionVar: "v1",
   180  							},
   181  							&v1.Application{
   182  								NameVar: "app3",
   183  								TypeVar: application.KubeApp,
   184  								FilesVar: []string{
   185  									"mysql.yaml",
   186  								},
   187  								VersionVar: "v1",
   188  							},
   189  						},
   190  						Launch: Launch{
   191  							Cmds: nil,
   192  							AppNames: []string{
   193  								"app1",
   194  								"app2",
   195  							},
   196  						},
   197  					},
   198  				},
   199  			},
   200  			want: `{"id":"783d1c3814b9cb28dafd7c3ca34b734d5798a61dc523c0d30349636e7cd56cc6","name":"cloud-image-registry.cn-shanghai.cr.aliyuncs.com/applications/1137465199671599:cnstack-acos-0.0.5-beta-07b8866-70f202","digest":"sha256:ebdd1badbb67f0f3de27317252618759db11782928bd206e9b155d5cc81f2076","manifestv1":{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","config":{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:783d1c3814b9cb28dafd7c3ca34b734d5798a61dc523c0d30349636e7cd56cc6","size":1288,"platform":{"architecture":"","os":""}},"layers":[{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:4e36d2017e0a3b3e6642471bbcefc8290fc92a173bd3a5e70d9285fe4af6cb51","size":3077,"platform":{"architecture":"","os":""}},{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:4086b218e01c8713ed47e93f8fd24c2e714a609ff3b5268c91231dd5590e58e4","size":288,"platform":{"architecture":"","os":""}}],"annotations":{"org.opencontainers.image.base.digest":"sha256:d3801a216e40f5bc517b7e8aa1da41c31127f819f38a49356ffbd23a3d476017","sealer.image.extension":"{\\\"type\\\":\\\"app-installer\\\",\\\"applications\\\":[{\\\"name\\\":\\\"cnstack-acos-0.0.5-beta\\\",\\\"type\\\":\\\"shell\\\",\\\"launchfiles\\\":[\\\"cnstack-acos-0.0.5-beta-install.sh\\\"],\\\"version\\\":\\\"v1\\\"}],\\\"launch\\\":{\\\"cmds\\\":[\\\"bash application/apps/cnstack-acos-0.0.5-beta/cnstack-acos-0.0.5-beta-install.sh\\\"]}}"}},"ociv1":{"created":"2022-12-14T13:02:50.93707+08:00","architecture":"amd64","os":"linux","config":{"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Labels":{"io.buildah.version":"1.27.1","io.sealer.version":"0.9"}},"rootfs":{"type":"layers","diff_ids":["sha256:aa735d9c504ee5a122bb323c0147a1ad317bd0e72b5eef1094025e793f6f4912","sha256:7c5cf4cbab5b83e603116d3b3f84287df4f8e70c3c35e3cd6d384b451bb8c26d","sha256:e0af0977fcb45ba29be4ef74ec1dcdbb625e01b78905bb089b8cc6b0ffc717ba"]},"history":[{"created":"2022-12-14T13:02:50.93707+08:00","created_by":"/bin/sh -c #(nop) COPY dir:2127a0f14734aff8fb3128a35824dcbb01dd2d0f8295e55e3ea0ab299daef971 in applications "},{"created":"2022-12-14T13:02:50.93707+08:00","created_by":"/bin/sh -c #(nop) COPY file:b48c5033638953a52b8c36a41734d2e862787520a3b372e4ef1161d491c7221f in application/apps/cnstack-acos-0.0.5-beta/ ","comment":"FROM c6dfe3236bb0"},{"created":"2022-12-14T13:02:50.93707+08:00","created_by":"/bin/sh -c #(nop) LABEL \"io.sealer.version\"=\"0.9\"","comment":"FROM 4a18afda8bf5","empty_layer":true},{"created":"2022-12-14T13:02:50.937071+08:00","created_by":"/bin/sh","comment":"FROM cloud-image-registry.cn-shanghai.cr.aliyuncs.com/applications/1137465199671599:cnstack-acos-0.0.5-beta-07b8866-70f202170cbcb3"}]},"buildClient":{"sealerVersion":"0.9","buildahVersion":"0.0.1"},"schemaVersion":"0.1","type":"app-image","applications":[{"name":"app1","type":"shell","launchfiles":["cnstack-acos-0.0.5-beta-install.sh"],"version":"v1"},{"name":"app2","type":"helm","launchfiles":["chart.tgz"],"version":"v1"},{"name":"app3","type":"kube","launchfiles":["mysql.yaml"],"version":"v1"}],"launch":{"app_names":["app1","app2"]}}`,
   201  		},
   202  	}
   203  	for _, tt := range tests {
   204  		t.Run(tt.name, func(t *testing.T) {
   205  			got, err := json.Marshal(tt.args.spec)
   206  			if err != nil {
   207  				t.Errorf("failed to marshal image spec: %v", err)
   208  				return
   209  			}
   210  			result := string(got)
   211  			if reflect.DeepEqual(result, tt.want) {
   212  				t.Errorf("NewImageSpec() = %v, want %v", result, tt.want)
   213  			}
   214  		})
   215  	}
   216  }
   217  
   218  func wrapTimePoint(t time.Time) *time.Time {
   219  	return &t
   220  }