From c6b5fb5ba336cc3160c637bab413342c15ffcfc5 Mon Sep 17 00:00:00 2001 From: Raffaele Di Fazio Date: Fri, 17 Sep 2021 14:55:50 +0200 Subject: [PATCH] add the tests Signed-off-by: Raffaele Di Fazio --- cmd/ghcs/delete_test.go | 103 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 cmd/ghcs/delete_test.go diff --git a/cmd/ghcs/delete_test.go b/cmd/ghcs/delete_test.go new file mode 100644 index 000000000..efb59124e --- /dev/null +++ b/cmd/ghcs/delete_test.go @@ -0,0 +1,103 @@ +package main + +import ( + "github.com/github/ghcs/internal/api" + "testing" + "time" +) + +func TestFilterCodespacesToDelete(t *testing.T) { + type args struct { + codespaces []*api.Codespace + thresholdDays int + } + tests := []struct { + name string + now time.Time + args args + wantErr bool + deleted []*api.Codespace + }{ + { + name: "no codespaces is to be deleted", + + args: args{ + codespaces: []*api.Codespace{ + { + Name: "testcodespace", + CreatedAt: "2021-08-09T10:10:24+02:00", + LastUsedAt: "2021-08-09T13:10:24+02:00", + Environment: api.CodespaceEnvironment{ + State: "Shutdown", + }, + }, + }, + thresholdDays: 1, + }, + now: time.Date(2021, 8, 9, 20, 10, 24, 0, time.UTC), + deleted: []*api.Codespace{}, + }, + { + name: "one codespace is to be deleted", + + args: args{ + codespaces: []*api.Codespace{ + { + Name: "testcodespace", + CreatedAt: "2021-08-09T10:10:24+02:00", + LastUsedAt: "2021-08-09T13:10:24+02:00", + Environment: api.CodespaceEnvironment{ + State: "Shutdown", + }, + }, + }, + thresholdDays: 1, + }, + now: time.Date(2021, 8, 15, 20, 12, 24, 0, time.UTC), + deleted: []*api.Codespace{ + { + Name: "testcodespace", + CreatedAt: "2021-08-09T10:10:24+02:00", + LastUsedAt: "2021-08-09T13:10:24+02:00", + }, + }, + }, + { + name: "threshold is invalid", + + args: args{ + codespaces: []*api.Codespace{ + { + Name: "testcodespace", + CreatedAt: "2021-08-09T10:10:24+02:00", + LastUsedAt: "2021-08-09T13:10:24+02:00", + Environment: api.CodespaceEnvironment{ + State: "Shutdown", + }, + }, + }, + thresholdDays: -1, + }, + now: time.Date(2021, 8, 15, 20, 12, 24, 0, time.UTC), + wantErr: true, + deleted: []*api.Codespace{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + now = func() time.Time { + return tt.now + } + + codespaces, err := filterCodespacesToDelete(tt.args.codespaces, tt.args.thresholdDays) + if (err != nil) != tt.wantErr { + t.Errorf("API.CleanupUnusedCodespaces() error = %v, wantErr %v", err, tt.wantErr) + } + + if len(codespaces) != len(tt.deleted) { + t.Errorf("expected %d deleted codespaces, got %d", len(tt.deleted), len(codespaces)) + } + }) + } +}