|
| 1 | +package akash_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/DATA-DOG/go-sqlmock" |
| 12 | + "github.com/jim380/Cendermint/config" |
| 13 | + "github.com/jim380/Cendermint/constants" |
| 14 | + "github.com/jim380/Cendermint/services/akash" |
| 15 | + "github.com/jim380/Cendermint/types" |
| 16 | + akash_types "github.com/jim380/Cendermint/types/akash" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func TestGetAkashDeployments(t *testing.T) { |
| 21 | + // Read deployments data |
| 22 | + deploymentsData, err := os.ReadFile("../../testutil/json/akash_deployments.json") |
| 23 | + require.NoError(t, err, "Failed to read akash_deployments.json") |
| 24 | + |
| 25 | + // Read active deployments data |
| 26 | + activeDeploymentsData, err := os.ReadFile("../../testutil/json/akash_active_deployments.json") |
| 27 | + require.NoError(t, err, "Failed to read akash_active_deployments.json") |
| 28 | + |
| 29 | + // Set up test server for deployments |
| 30 | + deploymentsServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 31 | + if r.URL.Query().Get("filters.state") == "active" { |
| 32 | + w.WriteHeader(http.StatusOK) |
| 33 | + w.Write(activeDeploymentsData) |
| 34 | + } else { |
| 35 | + w.WriteHeader(http.StatusOK) |
| 36 | + w.Write(deploymentsData) |
| 37 | + } |
| 38 | + })) |
| 39 | + defer deploymentsServer.Close() |
| 40 | + |
| 41 | + // Override REST address for deployments |
| 42 | + originalRESTAddr := constants.RESTAddr |
| 43 | + constants.RESTAddr = deploymentsServer.URL |
| 44 | + defer func() { constants.RESTAddr = originalRESTAddr }() |
| 45 | + |
| 46 | + // Initialize AkashService and call GetAkashDeployments |
| 47 | + as := &akash.AkashService{} |
| 48 | + cfg := config.Config{Chain: config.Chain{Name: "akash"}} |
| 49 | + data := &types.AsyncData{} |
| 50 | + |
| 51 | + deployments := as.GetAkashDeployments(cfg, data) |
| 52 | + |
| 53 | + // Assertions |
| 54 | + require.NotNil(t, deployments, "Deployments should not be nil") |
| 55 | + require.Equal(t, 306819, data.AkashInfo.TotalDeployments, "TotalDeployments mismatch") |
| 56 | + require.Equal(t, 7074, data.AkashInfo.ActiveDeployments, "ActiveDeployments mismatch") |
| 57 | + require.Equal(t, 306819-7074, data.AkashInfo.ClosedDeployments, "ClosedDeployments mismatch") |
| 58 | +} |
| 59 | + |
| 60 | +func TestIndexProviders(t *testing.T) { |
| 61 | + // Create mock database |
| 62 | + db, mock, err := sqlmock.New() |
| 63 | + require.NoError(t, err) |
| 64 | + defer db.Close() |
| 65 | + |
| 66 | + // Initialize AkashService with mock database |
| 67 | + as := &akash.AkashService{DB: db} |
| 68 | + cfg := config.Config{Chain: config.Chain{Name: "akash"}} |
| 69 | + |
| 70 | + // Sample providers data |
| 71 | + providers := akash_types.ProvidersResponse{ |
| 72 | + Providers: []akash_types.Provider{ |
| 73 | + { |
| 74 | + Owner: "akash1qphp30grcwp369uf5x5jsen56q2vtgc5l40prd", |
| 75 | + HostURI: "https://provider.spheron.wiki:8443", |
| 76 | + Attributes: []akash_types.Attribute{ |
| 77 | + {Key: "region", Value: "eu-east"}, |
| 78 | + {Key: "host", Value: "akash"}, |
| 79 | + {Key: "tier", Value: "community"}, |
| 80 | + {Key: "organization", Value: "spheron"}, |
| 81 | + {Key: "capabilities/storage/1/class", Value: "beta3"}, |
| 82 | + {Key: "capabilities/storage/1/persistent", Value: "true"}, |
| 83 | + }, |
| 84 | + Info: akash_types.Info{Email: "", Website: ""}, |
| 85 | + }, |
| 86 | + { |
| 87 | + Owner: "akash1qzkvrygsazvwyn7jm0r3efs26qkxde635cgvls", |
| 88 | + HostURI: "https://provider-test-14.testcoders.com:8443", |
| 89 | + Attributes: []akash_types.Attribute{ |
| 90 | + {Key: "host", Value: "Testcoders Test 14"}, |
| 91 | + }, |
| 92 | + Info: akash_types.Info{Email: "", Website: ""}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + // Expect database interactions |
| 98 | + for _, provider := range providers.Providers { |
| 99 | + mock.ExpectExec(`INSERT INTO akash_providers`). |
| 100 | + WithArgs(provider.Owner, provider.HostURI, provider.Info.Email, provider.Info.Website). |
| 101 | + WillReturnResult(sqlmock.NewResult(1, 1)) |
| 102 | + |
| 103 | + for _, attribute := range provider.Attributes { |
| 104 | + mock.ExpectExec(`INSERT INTO akash_provider_attributes`). |
| 105 | + WithArgs(provider.Owner, attribute.Key, attribute.Value). |
| 106 | + WillReturnResult(sqlmock.NewResult(1, 1)) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Call IndexProviders |
| 111 | + err = as.IndexProviders(cfg, providers) |
| 112 | + require.NoError(t, err) |
| 113 | + |
| 114 | + // Ensure all expectations were met |
| 115 | + err = mock.ExpectationsWereMet() |
| 116 | + require.NoError(t, err) |
| 117 | +} |
| 118 | + |
| 119 | +func TestFindProviderOwnersPendingAuditorUpdate(t *testing.T) { |
| 120 | + // Create mock database |
| 121 | + db, mock, err := sqlmock.New() |
| 122 | + require.NoError(t, err) |
| 123 | + defer db.Close() |
| 124 | + |
| 125 | + // Initialize AkashService with mock database |
| 126 | + as := &akash.AkashService{DB: db} |
| 127 | + |
| 128 | + // Define the period |
| 129 | + period := 30 * 24 * time.Hour // 30 days |
| 130 | + |
| 131 | + // Sample data to return from the query |
| 132 | + rows := sqlmock.NewRows([]string{"owner"}). |
| 133 | + AddRow("akash1qphp30grcwp369uf5x5jsen56q2vtgc5l40prd"). |
| 134 | + AddRow("akash1qzkvrygsazvwyn7jm0r3efs26qkxde635cgvls") |
| 135 | + |
| 136 | + // Expect the query to be executed |
| 137 | + mock.ExpectQuery(`SELECT owner FROM \( |
| 138 | + SELECT DISTINCT owner FROM akash_providers |
| 139 | + WHERE owner NOT IN \( |
| 140 | + SELECT DISTINCT provider_owner FROM akash_provider_auditors |
| 141 | + \) |
| 142 | + UNION |
| 143 | + SELECT provider_owner FROM akash_provider_auditors |
| 144 | + WHERE last_updated < \$1 |
| 145 | + \) AS subquery`). |
| 146 | + WithArgs(sqlmock.AnyArg()). // Use AnyArg to match any time argument |
| 147 | + WillReturnRows(rows) |
| 148 | + |
| 149 | + // Call FindProviderOwnersPendingAuditorUpdate |
| 150 | + providerOwners, err := as.FindProviderOwnersPendingAuditorUpdate(period) |
| 151 | + require.NoError(t, err) |
| 152 | + |
| 153 | + // Assertions |
| 154 | + require.Equal(t, 2, len(providerOwners), "Provider owners count mismatch") |
| 155 | + require.Equal(t, "akash1qphp30grcwp369uf5x5jsen56q2vtgc5l40prd", providerOwners[0], "First provider owner mismatch") |
| 156 | + require.Equal(t, "akash1qzkvrygsazvwyn7jm0r3efs26qkxde635cgvls", providerOwners[1], "Second provider owner mismatch") |
| 157 | + |
| 158 | + // Ensure all expectations were met |
| 159 | + err = mock.ExpectationsWereMet() |
| 160 | + require.NoError(t, err) |
| 161 | +} |
| 162 | + |
| 163 | +func TestIndexAuditorForProviderOwners(t *testing.T) { |
| 164 | + // Create mock database |
| 165 | + db, mock, err := sqlmock.New() |
| 166 | + require.NoError(t, err) |
| 167 | + defer db.Close() |
| 168 | + |
| 169 | + // Initialize AkashService with mock database |
| 170 | + as := &akash.AkashService{DB: db} |
| 171 | + cfg := config.Config{Chain: config.Chain{Name: "akash"}} |
| 172 | + |
| 173 | + // Sample provider owners |
| 174 | + providerOwners := []string{ |
| 175 | + "akash1qphp30grcwp369uf5x5jsen56q2vtgc5l40prd", |
| 176 | + "akash1qzkvrygsazvwyn7jm0r3efs26qkxde635cgvls", |
| 177 | + } |
| 178 | + |
| 179 | + // Sample auditors data |
| 180 | + auditorsData := akash_types.AuditorsResponse{ |
| 181 | + Providers: []struct { |
| 182 | + Auditor string `json:"auditor"` |
| 183 | + }{ |
| 184 | + {Auditor: "auditor1"}, |
| 185 | + {Auditor: "auditor2"}, |
| 186 | + }, |
| 187 | + Pagination: akash_types.Pagination{NextKey: "", Total: "2"}, |
| 188 | + } |
| 189 | + auditorsDataJSON, err := json.Marshal(auditorsData) |
| 190 | + require.NoError(t, err, "Failed to marshal auditors data") |
| 191 | + |
| 192 | + // Set up test server for auditors |
| 193 | + auditorsServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 194 | + w.WriteHeader(http.StatusOK) |
| 195 | + w.Write(auditorsDataJSON) |
| 196 | + })) |
| 197 | + defer auditorsServer.Close() |
| 198 | + |
| 199 | + // Override REST address for auditors |
| 200 | + originalRESTAddr := constants.RESTAddr |
| 201 | + constants.RESTAddr = auditorsServer.URL |
| 202 | + defer func() { constants.RESTAddr = originalRESTAddr }() |
| 203 | + |
| 204 | + // Expect database interactions |
| 205 | + for _, owner := range providerOwners { |
| 206 | + for _, auditor := range auditorsData.Providers { |
| 207 | + mock.ExpectExec(`INSERT INTO akash_provider_auditors`). |
| 208 | + WithArgs(owner, auditor.Auditor). |
| 209 | + WillReturnResult(sqlmock.NewResult(1, 1)) |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // Call IndexAuditorForProviderOwners |
| 214 | + err = as.IndexAuditorForProviderOwners(cfg, providerOwners) |
| 215 | + require.NoError(t, err) |
| 216 | + |
| 217 | + // Ensure all expectations were met |
| 218 | + err = mock.ExpectationsWereMet() |
| 219 | + require.NoError(t, err) |
| 220 | +} |
| 221 | + |
| 222 | +func TestFindProviderOwnersPendingDeploymentUpdate(t *testing.T) { |
| 223 | + // Create mock database |
| 224 | + db, mock, err := sqlmock.New() |
| 225 | + require.NoError(t, err) |
| 226 | + defer db.Close() |
| 227 | + |
| 228 | + // Initialize AkashService with mock database |
| 229 | + as := &akash.AkashService{DB: db} |
| 230 | + |
| 231 | + // Define the period |
| 232 | + period := 30 * 24 * time.Hour // 30 days |
| 233 | + |
| 234 | + // Sample data to return from the query |
| 235 | + rows := sqlmock.NewRows([]string{"owner"}). |
| 236 | + AddRow("akash1qphp30grcwp369uf5x5jsen56q2vtgc5l40prd"). |
| 237 | + AddRow("akash1qzkvrygsazvwyn7jm0r3efs26qkxde635cgvls") |
| 238 | + |
| 239 | + // Expect the query to be executed |
| 240 | + mock.ExpectQuery(`SELECT owner FROM \( |
| 241 | + SELECT DISTINCT owner FROM akash_providers |
| 242 | + WHERE owner NOT IN \( |
| 243 | + SELECT DISTINCT owner FROM akash_deployments |
| 244 | + \) |
| 245 | + UNION |
| 246 | + SELECT owner FROM akash_deployments |
| 247 | + WHERE last_updated < \$1 |
| 248 | + \) AS subquery`). |
| 249 | + WithArgs(sqlmock.AnyArg()). // Use AnyArg to match any time argument |
| 250 | + WillReturnRows(rows) |
| 251 | + |
| 252 | + // Call FindProviderOwnersPendingDeploymentUpdate |
| 253 | + providerOwners, err := as.FindProviderOwnersPendingDeploymentUpdate(period) |
| 254 | + require.NoError(t, err) |
| 255 | + |
| 256 | + // Assertions |
| 257 | + require.Equal(t, 2, len(providerOwners), "Provider owners count mismatch") |
| 258 | + require.Equal(t, "akash1qphp30grcwp369uf5x5jsen56q2vtgc5l40prd", providerOwners[0], "First provider owner mismatch") |
| 259 | + require.Equal(t, "akash1qzkvrygsazvwyn7jm0r3efs26qkxde635cgvls", providerOwners[1], "Second provider owner mismatch") |
| 260 | + |
| 261 | + // Ensure all expectations were met |
| 262 | + err = mock.ExpectationsWereMet() |
| 263 | + require.NoError(t, err) |
| 264 | +} |
0 commit comments