Skip to content

Commit

Permalink
docr: Add unit tests for new Registries commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rak16 committed Nov 14, 2024
1 parent ed343b9 commit 9eec56b
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 0 deletions.
3 changes: 3 additions & 0 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ type tcMocks struct {
projects *domocks.MockProjectsService
kubernetes *domocks.MockKubernetesService
registry *domocks.MockRegistryService
registries *domocks.MockRegistriesService
sshRunner *domocks.MockRunner
vpcs *domocks.MockVPCsService
oneClick *domocks.MockOneClickService
Expand Down Expand Up @@ -220,6 +221,7 @@ func withTestClient(t *testing.T, tFn testFn) {
kubernetes: domocks.NewMockKubernetesService(ctrl),
databases: domocks.NewMockDatabasesService(ctrl),
registry: domocks.NewMockRegistryService(ctrl),
registries: domocks.NewMockRegistriesService(ctrl),
sshRunner: domocks.NewMockRunner(ctrl),
vpcs: domocks.NewMockVPCsService(ctrl),
oneClick: domocks.NewMockOneClickService(ctrl),
Expand Down Expand Up @@ -279,6 +281,7 @@ func withTestClient(t *testing.T, tFn testFn) {
Kubernetes: func() do.KubernetesService { return tm.kubernetes },
Databases: func() do.DatabasesService { return tm.databases },
Registry: func() do.RegistryService { return tm.registry },
Registries: func() do.RegistriesService { return tm.registries },
VPCs: func() do.VPCsService { return tm.vpcs },
OneClicks: func() do.OneClickService { return tm.oneClick },
Apps: func() do.AppsService { return tm.apps },
Expand Down
204 changes: 204 additions & 0 deletions commands/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,3 +1085,207 @@ func TestGetAvailableRegions(t *testing.T) {
})
})
}

func TestRegistriesCreate(t *testing.T) {
t.Run("success", func(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
rcr := godo.RegistriesCreateRequest{
Name: testRegistryName,
Region: testRegistryRegion,
}
tm.registries.EXPECT().Create(&rcr).Return(&testRegistry, nil)
config.Args = append(config.Args, testRegistryName)
config.Doit.Set(config.NS, doctl.ArgRegionSlug, testRegistryRegion)

config.Registries = func() do.RegistriesService {
return tm.registries
}

err := RunRegistriesCreate(config)
assert.NoError(t, err)
})
})

t.Run("region omitted", func(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
rcr := godo.RegistriesCreateRequest{
Name: testRegistryName,
}
tm.registries.EXPECT().Create(&rcr).Return(&testRegistry, nil)
config.Args = append(config.Args, testRegistryName)

config.Registries = func() do.RegistriesService {
return tm.registries
}

err := RunRegistriesCreate(config)
assert.NoError(t, err)
})
})

t.Run("error", func(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
rcr := godo.RegistriesCreateRequest{
Name: testRegistryName,
Region: testRegistryRegion,
}
tm.registries.EXPECT().Create(&rcr).Return(nil, errTest)
config.Args = append(config.Args, testRegistryName)
config.Doit.Set(config.NS, doctl.ArgRegionSlug, testRegistryRegion)

config.Registries = func() do.RegistriesService {
return tm.registries
}

err := RunRegistriesCreate(config)
assert.Error(t, err)
assert.Equal(t, errTest, err)
})
})
}

func TestRegistriesGet(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.registries.EXPECT().Get(testRegistryName).Return(&testRegistry, nil)

config.Args = append(config.Args, testRegistryName)
config.Registries = func() do.RegistriesService {
return tm.registries
}

var buf bytes.Buffer
config.Out = &buf
err := RunRegistriesGet(config)
assert.NoError(t, err)
output := buf.String()
assert.Contains(t, output, testRegistry.Name)
})
}

func TestRegistriesDelete(t *testing.T) {
t.Run("success", func(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.registries.EXPECT().Delete(testRegistryName).Return(nil)

config.Args = append(config.Args, testRegistryName)
config.Doit.Set(config.NS, doctl.ArgForce, true)

config.Registries = func() do.RegistriesService {
return tm.registries
}

err := RunRegistriesDelete(config)
assert.NoError(t, err)
})
})

t.Run("error", func(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.registries.EXPECT().Delete(testRegistryName).Return(errTest)

config.Args = append(config.Args, testRegistryName)
config.Doit.Set(config.NS, doctl.ArgForce, true)

config.Registries = func() do.RegistriesService {
return tm.registries
}

err := RunRegistriesDelete(config)
assert.Error(t, err)
assert.Equal(t, errTest, err)
})
})
}

func TestRegistriesDockerConfig(t *testing.T) {
tests := []struct {
name string
readWrite bool
expirySeconds int
expect func(m *mocks.MockRegistriesService)
}{
{
name: "read-only-no-expiry",
readWrite: false,
expirySeconds: 0,
expect: func(m *mocks.MockRegistriesService) {
m.EXPECT().DockerCredentials(testRegistryName, &godo.RegistryDockerCredentialsRequest{
ReadWrite: false,
}).Return(testDockerCredentials, nil)
},
},
{
name: "read-write-no-expiry",
readWrite: true,
expirySeconds: 0,
expect: func(m *mocks.MockRegistriesService) {
m.EXPECT().DockerCredentials(testRegistryName, &godo.RegistryDockerCredentialsRequest{
ReadWrite: true,
}).Return(testDockerCredentials, nil)
},
},
{
name: "read-only-with-expiry",
readWrite: false,
expirySeconds: 3600,
expect: func(m *mocks.MockRegistriesService) {
m.EXPECT().DockerCredentials(testRegistryName, &godo.RegistryDockerCredentialsRequest{
ReadWrite: false,
ExpirySeconds: godo.PtrTo(3600),
}).Return(testDockerCredentials, nil)
},
},
{
name: "read-write-with-expiry",
readWrite: true,
expirySeconds: 3600,
expect: func(m *mocks.MockRegistriesService) {
m.EXPECT().DockerCredentials(testRegistryName, &godo.RegistryDockerCredentialsRequest{
ReadWrite: true,
ExpirySeconds: godo.PtrTo(3600),
}).Return(testDockerCredentials, nil)
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
if test.expect != nil {
test.expect(tm.registries)
}

config.Args = append(config.Args, testRegistryName)
config.Doit.Set(config.NS, doctl.ArgReadWrite, test.readWrite)
config.Doit.Set(config.NS, doctl.ArgRegistryExpirySeconds, test.expirySeconds)

config.Registries = func() do.RegistriesService {
return tm.registries
}

var output bytes.Buffer
config.Out = &output

err := RunRegistriesDockerConfig(config)
assert.NoError(t, err)

expectedOutput := append(testDockerCredentials.DockerConfigJSON, '\n')
assert.Equal(t, expectedOutput, output.Bytes())
})
})
}
}

func TestRegistriesList(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.registries.EXPECT().List().Return([]do.Registry{testRegistry}, nil)

var buf bytes.Buffer
config.Out = &buf
err := RunRegistriesList(config)
assert.NoError(t, err)

output := buf.String()
assert.Contains(t, output, testRegistry.Name)
})
}

0 comments on commit 9eec56b

Please sign in to comment.