|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package tools |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "encoding/json" |
| 23 | + "testing" |
| 24 | + |
| 25 | + meshapi "github.com/apache/dubbo-admin/api/mesh/v1alpha1" |
| 26 | + "github.com/apache/dubbo-admin/pkg/config/app" |
| 27 | + discoverycfg "github.com/apache/dubbo-admin/pkg/config/discovery" |
| 28 | + enginecfg "github.com/apache/dubbo-admin/pkg/config/engine" |
| 29 | + consolectx "github.com/apache/dubbo-admin/pkg/console/context" |
| 30 | + "github.com/apache/dubbo-admin/pkg/console/counter" |
| 31 | + "github.com/apache/dubbo-admin/pkg/core/lock" |
| 32 | + "github.com/apache/dubbo-admin/pkg/core/manager" |
| 33 | + meshresource "github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1" |
| 34 | + coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model" |
| 35 | + "github.com/apache/dubbo-admin/pkg/core/store/index" |
| 36 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 37 | +) |
| 38 | + |
| 39 | +func TestGetServiceDetailMissingServiceName(t *testing.T) { |
| 40 | + result, err := GetServiceDetail(newToolTestContext(nil), map[string]any{}) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("GetServiceDetail returned unexpected error: %v", err) |
| 43 | + } |
| 44 | + if !result.IsError { |
| 45 | + t.Fatal("Expected error result") |
| 46 | + } |
| 47 | + if got := result.Content[0].Text; got != "required parameter 'serviceName' is missing" { |
| 48 | + t.Fatalf("Expected missing parameter error, got %q", got) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestGetServiceDetailSuccess(t *testing.T) { |
| 53 | + const ( |
| 54 | + mesh = "mesh1" |
| 55 | + serviceName = "org.apache.demo.DemoService" |
| 56 | + version = "1.0.0" |
| 57 | + group = "demo" |
| 58 | + ) |
| 59 | + serviceKey := coremodel.BuildResourceKey(mesh, meshresource.BuildServiceIdentityKey(serviceName, version, group)) |
| 60 | + resource := &meshresource.ServiceResource{ |
| 61 | + ObjectMeta: metav1.ObjectMeta{Name: meshresource.BuildServiceIdentityKey(serviceName, version, group)}, |
| 62 | + Mesh: mesh, |
| 63 | + Spec: &meshapi.Service{ |
| 64 | + Name: serviceName, |
| 65 | + Version: version, |
| 66 | + Group: group, |
| 67 | + Language: "java", |
| 68 | + Methods: []string{"sayHello"}, |
| 69 | + }, |
| 70 | + } |
| 71 | + ctx := newToolTestContext(map[coremodel.ResourceKind]map[string]coremodel.Resource{ |
| 72 | + meshresource.ServiceKind: { |
| 73 | + serviceKey: resource, |
| 74 | + }, |
| 75 | + }) |
| 76 | + |
| 77 | + result, err := GetServiceDetail(ctx, map[string]any{ |
| 78 | + "serviceName": serviceName, |
| 79 | + "version": version, |
| 80 | + "group": group, |
| 81 | + "mesh": mesh, |
| 82 | + }) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("GetServiceDetail returned unexpected error: %v", err) |
| 85 | + } |
| 86 | + if result.IsError { |
| 87 | + t.Fatalf("Expected success result, got %q", result.Content[0].Text) |
| 88 | + } |
| 89 | + |
| 90 | + var payload struct { |
| 91 | + Language string `json:"language"` |
| 92 | + Methods []string `json:"methods"` |
| 93 | + } |
| 94 | + if err := json.Unmarshal([]byte(result.Content[0].Text), &payload); err != nil { |
| 95 | + t.Fatalf("Failed to unmarshal result: %v", err) |
| 96 | + } |
| 97 | + if payload.Language != "java" { |
| 98 | + t.Fatalf("Expected language java, got %q", payload.Language) |
| 99 | + } |
| 100 | + if len(payload.Methods) != 1 || payload.Methods[0] != "sayHello" { |
| 101 | + t.Fatalf("Expected methods [sayHello], got %v", payload.Methods) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestGetServiceDistributionSuccessWithEmptyDistribution(t *testing.T) { |
| 106 | + result, err := GetServiceDistribution(newToolTestContext(nil), map[string]any{"serviceName": "missing"}) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("get_service_distribution handler returned unexpected error: %v", err) |
| 109 | + } |
| 110 | + if result.IsError { |
| 111 | + t.Fatalf("Expected success result with empty distribution, got %q", result.Content[0].Text) |
| 112 | + } |
| 113 | + |
| 114 | + var payload struct { |
| 115 | + ServiceName string `json:"serviceName"` |
| 116 | + Distribution []any `json:"distribution"` |
| 117 | + TotalApps int `json:"totalApps"` |
| 118 | + } |
| 119 | + if err := json.Unmarshal([]byte(result.Content[0].Text), &payload); err != nil { |
| 120 | + t.Fatalf("Failed to unmarshal result: %v", err) |
| 121 | + } |
| 122 | + if payload.ServiceName != "missing" { |
| 123 | + t.Fatalf("Expected serviceName missing, got %q", payload.ServiceName) |
| 124 | + } |
| 125 | + if len(payload.Distribution) != 0 || payload.TotalApps != 0 { |
| 126 | + t.Fatalf("Expected empty distribution, got distribution=%v totalApps=%d", payload.Distribution, payload.TotalApps) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func newToolTestContext(resources map[coremodel.ResourceKind]map[string]coremodel.Resource) consolectx.Context { |
| 131 | + return &toolTestContext{ |
| 132 | + config: app.AdminConfig{ |
| 133 | + Discovery: []*discoverycfg.Config{{ID: "mesh1"}}, |
| 134 | + Engine: &enginecfg.Config{Name: "engine1"}, |
| 135 | + }, |
| 136 | + resourceManager: &toolTestResourceManager{resources: resources}, |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +type toolTestContext struct { |
| 141 | + config app.AdminConfig |
| 142 | + resourceManager manager.ResourceManager |
| 143 | +} |
| 144 | + |
| 145 | +func (c *toolTestContext) ResourceManager() manager.ResourceManager { |
| 146 | + return c.resourceManager |
| 147 | +} |
| 148 | + |
| 149 | +func (c *toolTestContext) CounterManager() counter.CounterManager { |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func (c *toolTestContext) Config() app.AdminConfig { |
| 154 | + return c.config |
| 155 | +} |
| 156 | + |
| 157 | +func (c *toolTestContext) AppContext() context.Context { |
| 158 | + return context.Background() |
| 159 | +} |
| 160 | + |
| 161 | +func (c *toolTestContext) LockManager() lock.Lock { |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +type toolTestResourceManager struct { |
| 166 | + resources map[coremodel.ResourceKind]map[string]coremodel.Resource |
| 167 | +} |
| 168 | + |
| 169 | +func (m *toolTestResourceManager) GetByKey(rk coremodel.ResourceKind, key string) (coremodel.Resource, bool, error) { |
| 170 | + byKind := m.resources[rk] |
| 171 | + if byKind == nil { |
| 172 | + return nil, false, nil |
| 173 | + } |
| 174 | + resource, ok := byKind[key] |
| 175 | + return resource, ok, nil |
| 176 | +} |
| 177 | + |
| 178 | +func (m *toolTestResourceManager) GetByKeys(rk coremodel.ResourceKind, keys []string) ([]coremodel.Resource, error) { |
| 179 | + byKind := m.resources[rk] |
| 180 | + result := make([]coremodel.Resource, 0, len(keys)) |
| 181 | + for _, key := range keys { |
| 182 | + if resource, ok := byKind[key]; ok { |
| 183 | + result = append(result, resource) |
| 184 | + } |
| 185 | + } |
| 186 | + return result, nil |
| 187 | +} |
| 188 | + |
| 189 | +func (m *toolTestResourceManager) ListByIndexes(coremodel.ResourceKind, []index.IndexCondition) ([]coremodel.Resource, error) { |
| 190 | + return nil, nil |
| 191 | +} |
| 192 | + |
| 193 | +func (m *toolTestResourceManager) PageListByIndexes(coremodel.ResourceKind, []index.IndexCondition, coremodel.PageReq) (*coremodel.PageData[coremodel.Resource], error) { |
| 194 | + return coremodel.NewPageData[coremodel.Resource](0, 0, 0, nil), nil |
| 195 | +} |
| 196 | + |
| 197 | +func (m *toolTestResourceManager) Add(coremodel.Resource) error { |
| 198 | + return nil |
| 199 | +} |
| 200 | + |
| 201 | +func (m *toolTestResourceManager) Update(coremodel.Resource) error { |
| 202 | + return nil |
| 203 | +} |
| 204 | + |
| 205 | +func (m *toolTestResourceManager) Upsert(coremodel.Resource) error { |
| 206 | + return nil |
| 207 | +} |
| 208 | + |
| 209 | +func (m *toolTestResourceManager) DeleteByKey(coremodel.ResourceKind, string, string) error { |
| 210 | + return nil |
| 211 | +} |
0 commit comments