mirror of
https://github.com/containers/podman.git
synced 2025-05-17 23:26:08 +08:00
chore: extract duplicate codes
Signed-off-by: xiaowu.zhu <xiaowu.zhu@daocloud.io>
This commit is contained in:
@ -90,21 +90,7 @@ func (ic *ContainerEngine) SecretInspect(ctx context.Context, nameOrIDs []string
|
||||
if secret.UpdatedAt.IsZero() {
|
||||
secret.UpdatedAt = secret.CreatedAt
|
||||
}
|
||||
report := &entities.SecretInfoReport{
|
||||
ID: secret.ID,
|
||||
CreatedAt: secret.CreatedAt,
|
||||
UpdatedAt: secret.UpdatedAt,
|
||||
Spec: entities.SecretSpec{
|
||||
Name: secret.Name,
|
||||
Driver: entities.SecretDriverSpec{
|
||||
Name: secret.Driver,
|
||||
Options: secret.DriverOptions,
|
||||
},
|
||||
Labels: secret.Labels,
|
||||
},
|
||||
SecretData: string(data),
|
||||
}
|
||||
reports = append(reports, report)
|
||||
reports = append(reports, secretToReportWithData(*secret, string(data)))
|
||||
}
|
||||
|
||||
return reports, errs, nil
|
||||
@ -126,19 +112,7 @@ func (ic *ContainerEngine) SecretList(ctx context.Context, opts entities.SecretL
|
||||
return nil, err
|
||||
}
|
||||
if result {
|
||||
reportItem := entities.SecretInfoReport{
|
||||
ID: secret.ID,
|
||||
CreatedAt: secret.CreatedAt,
|
||||
UpdatedAt: secret.CreatedAt,
|
||||
Spec: entities.SecretSpec{
|
||||
Name: secret.Name,
|
||||
Driver: entities.SecretDriverSpec{
|
||||
Name: secret.Driver,
|
||||
Options: secret.DriverOptions,
|
||||
},
|
||||
},
|
||||
}
|
||||
report = append(report, &reportItem)
|
||||
report = append(report, secretToReport(secret))
|
||||
}
|
||||
}
|
||||
return report, nil
|
||||
@ -188,3 +162,24 @@ func (ic *ContainerEngine) SecretExists(ctx context.Context, nameOrID string) (*
|
||||
|
||||
return &entities.BoolReport{Value: secret != nil}, nil
|
||||
}
|
||||
|
||||
func secretToReport(secret secrets.Secret) *entities.SecretInfoReport {
|
||||
return secretToReportWithData(secret, "")
|
||||
}
|
||||
|
||||
func secretToReportWithData(secret secrets.Secret, data string) *entities.SecretInfoReport {
|
||||
return &entities.SecretInfoReport{
|
||||
ID: secret.ID,
|
||||
CreatedAt: secret.CreatedAt,
|
||||
UpdatedAt: secret.UpdatedAt,
|
||||
Spec: entities.SecretSpec{
|
||||
Name: secret.Name,
|
||||
Driver: entities.SecretDriverSpec{
|
||||
Name: secret.Driver,
|
||||
Options: secret.DriverOptions,
|
||||
},
|
||||
Labels: secret.Labels,
|
||||
},
|
||||
SecretData: data,
|
||||
}
|
||||
}
|
||||
|
61
pkg/domain/infra/abi/secrets_test.go
Normal file
61
pkg/domain/infra/abi/secrets_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
package abi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/containers/common/pkg/secrets"
|
||||
"github.com/containers/podman/v4/pkg/domain/entities"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_secretToReport(t *testing.T) {
|
||||
type args struct {
|
||||
secret secrets.Secret
|
||||
secretData string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *entities.SecretInfoReport
|
||||
}{
|
||||
{
|
||||
name: "test secretToReport",
|
||||
args: args{
|
||||
secret: secrets.Secret{
|
||||
Name: "test-name",
|
||||
ID: "test-id",
|
||||
Labels: map[string]string{
|
||||
"test-label": "test-value",
|
||||
},
|
||||
CreatedAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
UpdatedAt: time.Date(2024, 2, 3, 0, 0, 0, 0, time.UTC),
|
||||
Driver: "test-driver",
|
||||
DriverOptions: map[string]string{
|
||||
"test-driver-option": "test-value",
|
||||
},
|
||||
},
|
||||
secretData: "test-secret-data",
|
||||
},
|
||||
want: &entities.SecretInfoReport{
|
||||
ID: "test-id",
|
||||
CreatedAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
UpdatedAt: time.Date(2024, 2, 3, 0, 0, 0, 0, time.UTC),
|
||||
Spec: entities.SecretSpec{
|
||||
Name: "test-name",
|
||||
Driver: entities.SecretDriverSpec{
|
||||
Name: "test-driver",
|
||||
Options: map[string]string{"test-driver-option": "test-value"},
|
||||
},
|
||||
Labels: map[string]string{"test-label": "test-value"},
|
||||
},
|
||||
SecretData: "test-secret-data",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equalf(t, tt.want, secretToReportWithData(tt.args.secret, tt.args.secretData), "secretToReport(%v)", tt.args.secret)
|
||||
})
|
||||
}
|
||||
}
|
74
pkg/domain/utils/scp_test.go
Normal file
74
pkg/domain/utils/scp_test.go
Normal file
@ -0,0 +1,74 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/containers/podman/v4/pkg/domain/entities"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateSCPArgs(t *testing.T) {
|
||||
type args struct {
|
||||
locations []*entities.ImageScpOptions
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr assert.ErrorAssertionFunc
|
||||
}{
|
||||
{
|
||||
name: "test args length more than 2",
|
||||
args: args{
|
||||
locations: []*entities.ImageScpOptions{
|
||||
{
|
||||
Image: "source image one",
|
||||
},
|
||||
{
|
||||
Image: "source image two",
|
||||
},
|
||||
{
|
||||
Image: "target image one",
|
||||
},
|
||||
{
|
||||
Image: "target image two",
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: assert.Error,
|
||||
},
|
||||
{
|
||||
name: "test source image is empty",
|
||||
args: args{
|
||||
locations: []*entities.ImageScpOptions{
|
||||
{
|
||||
Image: "",
|
||||
},
|
||||
{
|
||||
Image: "target image",
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "test target image is empty",
|
||||
args: args{
|
||||
locations: []*entities.ImageScpOptions{
|
||||
{
|
||||
Image: "source image",
|
||||
},
|
||||
{
|
||||
Image: "target image",
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.wantErr(t, ValidateSCPArgs(tt.args.locations), fmt.Sprintf("ValidateSCPArgs(%v)", tt.args.locations))
|
||||
})
|
||||
}
|
||||
}
|
24
pkg/env/env_test.go
vendored
24
pkg/env/env_test.go
vendored
@ -160,3 +160,27 @@ func Test_parseEnv(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
type args struct {
|
||||
slice []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want map[string]string
|
||||
}{
|
||||
{
|
||||
name: "test parse env to map",
|
||||
args: args{
|
||||
slice: []string{"apple=red", "banana=yellow", "pear"},
|
||||
},
|
||||
want: map[string]string{"apple": "red", "banana": "yellow", "pear": ""},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equalf(t, tt.want, Map(tt.args.slice), "Map(%v)", tt.args.slice)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user