-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package delete | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
|
||
storage "github.com/ninech/apis/storage/v1alpha1" | ||
"github.com/ninech/nctl/api" | ||
) | ||
|
||
type postgresCmd struct { | ||
Name string `arg:"" help:"Name of the PostgreSQL resource."` | ||
Force bool `default:"false" help:"Do not ask for confirmation of deletion."` | ||
Wait bool `default:"true" help:"Wait until PostgreSQL is fully deleted."` | ||
WaitTimeout time.Duration `default:"300s" help:"Duration to wait for the deletion. Only relevant if wait is set."` | ||
} | ||
|
||
func (cmd *postgresCmd) Run(ctx context.Context, client *api.Client) error { | ||
ctx, cancel := context.WithTimeout(ctx, cmd.WaitTimeout) | ||
defer cancel() | ||
|
||
postgres := &storage.Postgres{} | ||
postgresName := types.NamespacedName{Name: cmd.Name, Namespace: client.Project} | ||
if err := client.Get(ctx, postgresName, postgres); err != nil { | ||
return fmt.Errorf("unable to get postgres %q: %w", postgres.Name, err) | ||
} | ||
|
||
return newDeleter(postgres, storage.PostgresKind).deleteResource(ctx, client, cmd.WaitTimeout, cmd.Wait, cmd.Force) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package delete | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ninech/nctl/api" | ||
"github.com/ninech/nctl/internal/test" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
func TestPostgres(t *testing.T) { | ||
cmd := postgresCmd{ | ||
Name: "test", | ||
Force: true, | ||
Wait: false, | ||
WaitTimeout: time.Second, | ||
} | ||
|
||
postgres := test.Postgres("test", "default", "nine-es34") | ||
|
||
scheme, err := api.NewScheme() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
client := fake.NewClientBuilder().WithScheme(scheme).Build() | ||
apiClient := &api.Client{WithWatch: client, Project: "default"} | ||
ctx := context.Background() | ||
|
||
if err := apiClient.Create(ctx, postgres); err != nil { | ||
t.Fatalf("postgres create error, got: %s", err) | ||
} | ||
if err := apiClient.Get(ctx, api.ObjectName(postgres), postgres); err != nil { | ||
t.Fatalf("expected postgres to exist, got: %s", err) | ||
} | ||
if err := cmd.Run(ctx, apiClient); err != nil { | ||
t.Fatal(err) | ||
} | ||
err = apiClient.Get(ctx, api.ObjectName(postgres), postgres) | ||
if err == nil { | ||
t.Fatalf("expected postgres to be deleted, but exists") | ||
} | ||
if !errors.IsNotFound(err) { | ||
t.Fatalf("expected postgres to be deleted, got: %s", err.Error()) | ||
} | ||
} |