2024-05-06 00:48:54 +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"
|
|
|
|
_ "github.com/docker/docker/api/types/container"
|
2024-07-17 08:34:57 +00:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2024-05-06 00:48:54 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
_ "io"
|
|
|
|
_ "net/http"
|
|
|
|
_ "net/http/httptest"
|
|
|
|
_ "net/url"
|
|
|
|
_ "os"
|
|
|
|
_ "strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewContainerNetworkResource(t *testing.T) {
|
|
|
|
c := NewContainerNetwork(&mocks.MockContainerClient{})
|
|
|
|
assert.NotNil(t, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadContainerNetwork(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
decl := `
|
|
|
|
name: "testcontainernetwork"
|
|
|
|
state: present
|
|
|
|
`
|
|
|
|
m := &mocks.MockContainerClient{
|
2024-07-17 08:34:57 +00:00
|
|
|
InjectNetworkList: func(ctx context.Context, options network.ListOptions) ([]network.Summary, error) {
|
|
|
|
return []network.Summary{
|
|
|
|
{ID: "123456789abc"},
|
|
|
|
{ID: "123456789def"},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
InjectNetworkInspect: func(ctx context.Context, networkID string, options network.InspectOptions) (network.Inspect, error) {
|
|
|
|
return network.Inspect{
|
|
|
|
ID: "123456789abc",
|
|
|
|
Name: "test",
|
|
|
|
Driver: "bridge",
|
|
|
|
}, nil
|
|
|
|
},
|
2024-05-06 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n := NewContainerNetwork(m)
|
|
|
|
assert.NotNil(t, n)
|
|
|
|
|
|
|
|
e := n.LoadDecl(decl)
|
|
|
|
assert.Nil(t, e)
|
|
|
|
assert.Equal(t, "testcontainernetwork", n.Name)
|
|
|
|
|
|
|
|
resourceYaml, readContainerErr := n.Read(ctx)
|
|
|
|
assert.Equal(t, nil, readContainerErr)
|
|
|
|
assert.Greater(t, len(resourceYaml), 0)
|
|
|
|
}
|