Skip to content

Commit 413a6b2

Browse files
committed
feature(api): add migration to insert vpn default value
1 parent 47be8e3 commit 413a6b2

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

api/store/mongo/migrations/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func GenerateMigrations() []migrate.Migration {
8484
migration72,
8585
migration73,
8686
migration74,
87+
migration75,
8788
}
8889
}
8990

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
"github.com/shellhub-io/shellhub/pkg/envs"
7+
"github.com/sirupsen/logrus"
8+
migrate "github.com/xakep666/mongo-migrate"
9+
"go.mongodb.org/mongo-driver/bson"
10+
"go.mongodb.org/mongo-driver/mongo"
11+
)
12+
13+
var migration75 = migrate.Migration{
14+
Version: 75,
15+
Description: "Adding VPN settings to namespace",
16+
Up: migrate.MigrationFunc(func(ctx context.Context, db *mongo.Database) error {
17+
logrus.WithFields(logrus.Fields{
18+
"component": "migration",
19+
"version": 75,
20+
"action": "Up",
21+
}).Info("Applying migration")
22+
23+
if envs.IsEnterprise() {
24+
update := bson.M{
25+
"$set": bson.M{
26+
"vpn": bson.M{
27+
"enable": false,
28+
"address": bson.A{10, 0, 0, 0},
29+
"mask": 16,
30+
},
31+
},
32+
}
33+
34+
_, err := db.
35+
Collection("namespaces").
36+
UpdateMany(ctx, bson.M{}, update)
37+
38+
return err
39+
}
40+
41+
return nil
42+
}),
43+
Down: migrate.MigrationFunc(func(ctx context.Context, db *mongo.Database) error {
44+
logrus.WithFields(logrus.Fields{
45+
"component": "migration",
46+
"version": 75,
47+
"action": "Down",
48+
}).Info("Reverting migration")
49+
50+
if envs.IsEnterprise() {
51+
update := bson.M{
52+
"$unset": bson.M{"vpn": ""},
53+
}
54+
55+
_, err := db.
56+
Collection("namespaces").
57+
UpdateMany(ctx, bson.M{}, update)
58+
59+
return err
60+
}
61+
62+
return nil
63+
}),
64+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)