// Copyright 2024 Matthew Rich . All rights reserved. package resource import ( "context" "decl/tests/mocks" _ "encoding/json" _ "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/image" "github.com/stretchr/testify/assert" "io" _ "net/http" _ "net/http/httptest" "net/url" _ "os" "strings" "testing" ) func TestNewContainerImageResource(t *testing.T) { c := NewContainerImage(&mocks.MockContainerClient{}) assert.NotNil(t, c) } func TestContainerImageURI(t *testing.T) { case0URI := URIFromContainerImageName("foo") assert.Equal(t, "container-image:///foo", case0URI) case1URI := URIFromContainerImageName("foo:bar") assert.Equal(t, "container-image:///foo:bar", case1URI) case2URI := URIFromContainerImageName("quuz/foo:bar") assert.Equal(t, "container-image:///quuz/foo:bar", case2URI) case3URI := URIFromContainerImageName("myhost/quuz/foo:bar") assert.Equal(t, "container-image://myhost/quuz/foo:bar", case3URI) } func TestLoadFromContainerImageURI(t *testing.T) { testURI := URIFromContainerImageName("myhost/quuz/foo:bar") newResource, resourceErr := ResourceTypes.New(testURI) assert.Nil(t, resourceErr) assert.NotNil(t, newResource) assert.IsType(t, &ContainerImage{}, newResource) assert.Equal(t, "myhost/quuz/foo:bar", newResource.(*ContainerImage).Name) assert.Equal(t, testURI, newResource.URI()) } func TestContainerImageNameFromURI(t *testing.T) { case0u,_ := url.Parse("container-image:///foo") case0Image := ContainerImageNameFromURI(case0u) assert.Equal(t, "foo", case0Image) case1u,_ := url.Parse("container-image:///foo:bar") case1Image := ContainerImageNameFromURI(case1u) assert.Equal(t, "foo:bar", case1Image) case2u,_ := url.Parse("container-image:///quuz/foo:bar") case2Image := ContainerImageNameFromURI(case2u) assert.Equal(t, "quuz/foo:bar", case2Image) case3u,_ := url.Parse("container-image://myhost/quuz/foo:bar") case3Image := ContainerImageNameFromURI(case3u) assert.Equal(t, "myhost/quuz/foo:bar", case3Image) } func TestReadContainerImage(t *testing.T) { output := io.NopCloser(strings.NewReader("testdata")) ctx := context.Background() decl := ` name: "alpine:latest" state: present ` m := &mocks.MockContainerClient{ InjectImagePull: func(ctx context.Context, refStr string, options image.PullOptions) (io.ReadCloser, error) { return output, nil }, InjectImageRemove: func(ctx context.Context, imageID string, options image.RemoveOptions) ([]image.DeleteResponse, error) { return nil, nil }, InjectImageInspectWithRaw: func(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) { return types.ImageInspect{ ID: "sha256:123456789abc", }, nil, nil }, } c := NewContainerImage(m) assert.NotNil(t, c) e := c.LoadDecl(decl) assert.Equal(t, nil, e) assert.Equal(t, "alpine:latest", c.Name) resourceYaml, readContainerErr := c.Read(ctx) assert.Equal(t, nil, readContainerErr) assert.Greater(t, len(resourceYaml), 0) } /* func TestCreateContainerImage(t *testing.T) { m := &mocks.MockContainerClient{ InjectContainerCreate: func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error) { return container.CreateResponse{ID: "abcdef012", Warnings: []string{}}, nil }, InjectContainerRemove: func(context.Context, string, container.RemoveOptions) error { return nil }, } decl := ` name: "testcontainer" image: "alpine" state: present ` c := NewContainerImage(m) e := c.LoadDecl(decl) assert.Equal(t, nil, e) assert.Equal(t, "testcontainer", c.Name) applyErr := c.Apply() assert.Equal(t, nil, applyErr) c.State = "absent" applyDeleteErr := c.Apply() assert.Equal(t, nil, applyDeleteErr) } */