|
| 1 | +package migrations |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/shellhub-io/shellhub/pkg/envs" |
| 9 | + env_mocks "github.com/shellhub-io/shellhub/pkg/envs/mocks" |
| 10 | + "github.com/shellhub-io/shellhub/pkg/models" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + migrate "github.com/xakep666/mongo-migrate" |
| 13 | + "go.mongodb.org/mongo-driver/bson" |
| 14 | +) |
| 15 | + |
| 16 | +func TestMigration75(t *testing.T) { |
| 17 | + ctx := context.Background() |
| 18 | + |
| 19 | + envMock = &env_mocks.Backend{} |
| 20 | + envs.DefaultBackend = envMock |
| 21 | + |
| 22 | + cases := []struct { |
| 23 | + description string |
| 24 | + setup func() error |
| 25 | + requireMocks func() |
| 26 | + test func() error |
| 27 | + }{ |
| 28 | + { |
| 29 | + description: "Success to apply up on migration 75", |
| 30 | + setup: func() error { |
| 31 | + _, err := c. |
| 32 | + Database("test"). |
| 33 | + Collection("namespaces"). |
| 34 | + InsertOne(ctx, models.Namespace{ |
| 35 | + TenantID: "00000000-0000-4000-0000-000000000000", |
| 36 | + }) |
| 37 | + |
| 38 | + return err |
| 39 | + }, |
| 40 | + requireMocks: func() { |
| 41 | + envMock.On("Get", "SHELLHUB_ENTERPRISE").Return("true").Once() |
| 42 | + }, |
| 43 | + test: func() error { |
| 44 | + migrations := GenerateMigrations()[74:75] |
| 45 | + migrates := migrate.NewMigrate(c.Database("test"), migrations...) |
| 46 | + err := migrates.Up(context.Background(), migrate.AllAvailable) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + query := c. |
| 52 | + Database("test"). |
| 53 | + Collection("namespaces"). |
| 54 | + FindOne(context.TODO(), bson.M{"tenant_id": "00000000-0000-4000-0000-000000000000"}) |
| 55 | + |
| 56 | + ns := new(models.Namespace) |
| 57 | + if err := query.Decode(ns); err != nil { |
| 58 | + return errors.New("unable to find the namespace") |
| 59 | + } |
| 60 | + |
| 61 | + if ns.VPN == nil { |
| 62 | + return errors.New("unable to apply the migration") |
| 63 | + } |
| 64 | + |
| 65 | + return nil |
| 66 | + }, |
| 67 | + }, { |
| 68 | + description: "Success to unapply the migration 75", |
| 69 | + setup: func() error { |
| 70 | + _, err := c. |
| 71 | + Database("test"). |
| 72 | + Collection("namespaces"). |
| 73 | + InsertOne(ctx, models.Namespace{ |
| 74 | + TenantID: "00000000-0000-4000-0000-000000000000", |
| 75 | + Settings: &models.NamespaceSettings{}, |
| 76 | + }) |
| 77 | + |
| 78 | + return err |
| 79 | + }, |
| 80 | + requireMocks: func() {}, |
| 81 | + test: func() error { |
| 82 | + migrations := GenerateMigrations()[74:75] |
| 83 | + migrates := migrate.NewMigrate(c.Database("test"), migrations...) |
| 84 | + err := migrates.Down(context.Background(), migrate.AllAvailable) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + query := c. |
| 90 | + Database("test"). |
| 91 | + Collection("namespaces"). |
| 92 | + FindOne(context.TODO(), bson.M{"tenant_id": "00000000-0000-4000-0000-000000000000"}) |
| 93 | + |
| 94 | + ns := new(models.Namespace) |
| 95 | + if err := query.Decode(ns); err != nil { |
| 96 | + return errors.New("unable to find the namespace") |
| 97 | + } |
| 98 | + |
| 99 | + if ns.VPN != nil { |
| 100 | + return errors.New("unable to unapply the migration") |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for _, test := range cases { |
| 109 | + tc := test |
| 110 | + t.Run(tc.description, func(t *testing.T) { |
| 111 | + tc.requireMocks() |
| 112 | + |
| 113 | + t.Cleanup(func() { |
| 114 | + assert.NoError(t, srv.Reset()) |
| 115 | + }) |
| 116 | + |
| 117 | + assert.NoError(t, tc.setup()) |
| 118 | + assert.NoError(t, tc.test()) |
| 119 | + |
| 120 | + envMock.AssertExpectations(t) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
0 commit comments