jx/internal/resource/container_image_test.go

90 lines
2.3 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"
_ "net/url"
_ "os"
"strings"
"testing"
)
func TestNewContainerImageResource(t *testing.T) {
c := NewContainerImage(&mocks.MockContainerClient{})
assert.NotNil(t, c)
}
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{
InjectImagePull: func(ctx context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error) {
return output, nil
},
2024-05-26 07:15:50 +00:00
InjectImageRemove: func(ctx context.Context, imageID string, options types.ImageRemoveOptions) ([]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)
}
*/