| 1 | package mg17 |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/ipfs/kubo/repo/fsrepo/migrations/common" |
| 7 | ) |
| 8 | |
| 9 | func TestMigration17to18(t *testing.T) { |
| 10 | migration := NewMigration() |
| 11 | |
| 12 | testCases := []common.TestCase{ |
| 13 | { |
| 14 | Name: "Migrate Provider and Reprovider to Provide", |
| 15 | InputConfig: common.GenerateTestConfig(map[string]any{ |
| 16 | "Provider": map[string]any{ |
| 17 | "Enabled": true, |
| 18 | "WorkerCount": 8, |
| 19 | "Strategy": "unused", // This field was unused and should be ignored |
| 20 | }, |
| 21 | "Reprovider": map[string]any{ |
| 22 | "Strategy": "pinned", |
| 23 | "Interval": "12h", |
| 24 | }, |
| 25 | }), |
| 26 | Assertions: []common.ConfigAssertion{ |
| 27 | {Path: "Provide.Enabled", Expected: true}, |
| 28 | {Path: "Provide.DHT.MaxWorkers", Expected: float64(8)}, // JSON unmarshals to float64 |
| 29 | {Path: "Provide.Strategy", Expected: "pinned"}, |
| 30 | {Path: "Provide.DHT.Interval", Expected: "12h"}, |
| 31 | {Path: "Provider", Expected: nil}, // Should be deleted |
| 32 | {Path: "Reprovider", Expected: nil}, // Should be deleted |
| 33 | }, |
| 34 | }, |
| 35 | { |
| 36 | Name: "Convert flat strategy to all", |
| 37 | InputConfig: common.GenerateTestConfig(map[string]any{ |
| 38 | "Provider": map[string]any{ |
| 39 | "Enabled": false, |
| 40 | }, |
| 41 | "Reprovider": map[string]any{ |
| 42 | "Strategy": "flat", // Deprecated, should be converted to "all" |
| 43 | "Interval": "24h", |
| 44 | }, |
| 45 | }), |
| 46 | Assertions: []common.ConfigAssertion{ |
| 47 | {Path: "Provide.Enabled", Expected: false}, |
| 48 | {Path: "Provide.Strategy", Expected: "all"}, // "flat" converted to "all" |
| 49 | {Path: "Provide.DHT.Interval", Expected: "24h"}, |
| 50 | {Path: "Provider", Expected: nil}, |
| 51 | {Path: "Reprovider", Expected: nil}, |
| 52 | }, |
| 53 | }, |
| 54 | { |
| 55 | Name: "Handle missing Provider section", |
| 56 | InputConfig: common.GenerateTestConfig(map[string]any{ |
| 57 | "Reprovider": map[string]any{ |
| 58 | "Strategy": "roots", |
| 59 | "Interval": "6h", |
| 60 | }, |
| 61 | }), |
| 62 | Assertions: []common.ConfigAssertion{ |
| 63 | {Path: "Provide.Strategy", Expected: "roots"}, |
| 64 | {Path: "Provide.DHT.Interval", Expected: "6h"}, |
| 65 | {Path: "Provider", Expected: nil}, |
| 66 | {Path: "Reprovider", Expected: nil}, |
| 67 | }, |
| 68 | }, |
| 69 | { |
| 70 | Name: "Handle missing Reprovider section", |
| 71 | InputConfig: common.GenerateTestConfig(map[string]any{ |
| 72 | "Provider": map[string]any{ |
| 73 | "Enabled": true, |
| 74 | "WorkerCount": 16, |
| 75 | }, |
| 76 | }), |
| 77 | Assertions: []common.ConfigAssertion{ |
| 78 | {Path: "Provide.Enabled", Expected: true}, |
| 79 | {Path: "Provide.DHT.MaxWorkers", Expected: float64(16)}, |
| 80 | {Path: "Provider", Expected: nil}, |
| 81 | {Path: "Reprovider", Expected: nil}, |
| 82 | }, |
| 83 | }, |
| 84 | { |
| 85 | Name: "Handle empty Provider and Reprovider sections", |
| 86 | InputConfig: common.GenerateTestConfig(map[string]any{ |
| 87 | "Provider": map[string]any{}, |
| 88 | "Reprovider": map[string]any{}, |
| 89 | }), |
| 90 | Assertions: []common.ConfigAssertion{ |
| 91 | {Path: "Provide", Expected: nil}, // No fields to migrate |
| 92 | {Path: "Provider", Expected: nil}, |
| 93 | {Path: "Reprovider", Expected: nil}, |
| 94 | }, |
| 95 | }, |
| 96 | { |
| 97 | Name: "Handle missing both sections", |
| 98 | InputConfig: common.GenerateTestConfig(map[string]any{ |
| 99 | "Datastore": map[string]any{ |
| 100 | "StorageMax": "10GB", |
| 101 | }, |
| 102 | }), |
| 103 | Assertions: []common.ConfigAssertion{ |
| 104 | {Path: "Provide", Expected: nil}, // No Provider/Reprovider to migrate |
| 105 | {Path: "Provider", Expected: nil}, |
| 106 | {Path: "Reprovider", Expected: nil}, |
| 107 | {Path: "Datastore.StorageMax", Expected: "10GB"}, // Other config preserved |
| 108 | }, |
| 109 | }, |
| 110 | { |
| 111 | Name: "Preserve other config sections", |
| 112 | InputConfig: common.GenerateTestConfig(map[string]any{ |
| 113 | "Provider": map[string]any{ |
| 114 | "Enabled": true, |
| 115 | }, |
| 116 | "Reprovider": map[string]any{ |
| 117 | "Strategy": "all", |
| 118 | }, |
| 119 | "Swarm": map[string]any{ |
| 120 | "ConnMgr": map[string]any{ |
| 121 | "Type": "basic", |
| 122 | }, |
| 123 | }, |
| 124 | }), |
| 125 | Assertions: []common.ConfigAssertion{ |
| 126 | {Path: "Provide.Enabled", Expected: true}, |
| 127 | {Path: "Provide.Strategy", Expected: "all"}, |
| 128 | {Path: "Swarm.ConnMgr.Type", Expected: "basic"}, // Other config preserved |
| 129 | {Path: "Provider", Expected: nil}, |
| 130 | {Path: "Reprovider", Expected: nil}, |
| 131 | }, |
| 132 | }, |
| 133 | } |
| 134 | |
| 135 | for _, tc := range testCases { |
| 136 | t.Run(tc.Name, func(t *testing.T) { |
| 137 | common.RunMigrationTest(t, migration, tc) |
| 138 | }) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func TestMigration17to18Reversible(t *testing.T) { |
| 143 | migration := NewMigration() |
| 144 | |
| 145 | // Test that migration is reversible |
| 146 | inputConfig := common.GenerateTestConfig(map[string]any{ |
| 147 | "Provide": map[string]any{ |
| 148 | "Enabled": true, |
| 149 | "WorkerCount": 8, |
| 150 | "Strategy": "pinned", |
| 151 | "Interval": "12h", |
| 152 | }, |
| 153 | }) |
| 154 | |
| 155 | // Test full migration and revert |
| 156 | migratedConfig := common.AssertMigrationSuccess(t, migration, 17, 18, inputConfig) |
| 157 | |
| 158 | // Check that Provide section exists after migration |
| 159 | common.AssertConfigField(t, migratedConfig, "Provide.Enabled", true) |
| 160 | |
| 161 | // Test revert |
| 162 | common.AssertMigrationReversible(t, migration, 17, 18, migratedConfig) |
| 163 | } |
| 164 | |
| 165 | func TestMigration17to18Integration(t *testing.T) { |
| 166 | migration := NewMigration() |
| 167 | |
| 168 | // Test that the migration properly integrates with the common framework |
| 169 | if migration.Versions() != "17-to-18" { |
| 170 | t.Errorf("expected versions '17-to-18', got '%s'", migration.Versions()) |
| 171 | } |
| 172 | |
| 173 | if !migration.Reversible() { |
| 174 | t.Error("migration should be reversible") |
| 175 | } |
| 176 | } |