90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
|
// 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"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"io"
|
||
|
"io/ioutil"
|
||
|
_ "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) {
|
||
|
output := ioutil.NopCloser(strings.NewReader("testdata"))
|
||
|
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
|
||
|
},
|
||
|
InjectImageRemove: func(ctx context.Context, imageID string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, 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)
|
||
|
}
|
||
|
*/
|