jx/internal/resource/container_image_test.go

129 lines
3.8 KiB
Go
Raw Normal View History

2024-05-24 05:11:51 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package resource
import (
"context"
"decl/tests/mocks"
_ "encoding/json"
_ "fmt"
"github.com/docker/docker/api/types"
2024-05-26 07:15:50 +00:00
"github.com/docker/docker/api/types/image"
2024-05-24 05:11:51 +00:00
"github.com/stretchr/testify/assert"
"io"
_ "net/http"
_ "net/http/httptest"
2024-07-17 08:34:57 +00:00
"net/url"
2024-05-24 05:11:51 +00:00
_ "os"
"strings"
"testing"
)
func TestNewContainerImageResource(t *testing.T) {
c := NewContainerImage(&mocks.MockContainerClient{})
assert.NotNil(t, c)
}
2024-07-17 08:34:57 +00:00
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)
}
2024-05-24 05:11:51 +00:00
func TestReadContainerImage(t *testing.T) {
2024-05-26 07:15:50 +00:00
output := io.NopCloser(strings.NewReader("testdata"))
2024-05-24 05:11:51 +00:00
ctx := context.Background()
decl := `
name: "alpine:latest"
state: present
`
m := &mocks.MockContainerClient{
2024-07-17 08:34:57 +00:00
InjectImagePull: func(ctx context.Context, refStr string, options image.PullOptions) (io.ReadCloser, error) {
2024-05-24 05:11:51 +00:00
return output, nil
},
2024-07-17 08:34:57 +00:00
InjectImageRemove: func(ctx context.Context, imageID string, options image.RemoveOptions) ([]image.DeleteResponse, error) {
2024-05-24 05:11:51 +00:00
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)
}
*/