|
| 1 | +package scenarios |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/cloudmap" |
| 7 | + "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/controllers" |
| 8 | + "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/model" |
| 9 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | + "k8s.io/apimachinery/pkg/util/wait" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + defaultScenarioPollInterval = 10 * time.Second |
| 18 | + defaultScenarioPollTimeout = 2 * time.Minute |
| 19 | +) |
| 20 | + |
| 21 | +// ExportServiceScenario defines an integration test against a service export to check creation of namespace, service, |
| 22 | +// and endpoint export. |
| 23 | +type ExportServiceScenario interface { |
| 24 | + // Run executes the service export integration test scenario, returning any error. |
| 25 | + Run() error |
| 26 | +} |
| 27 | + |
| 28 | +type exportServiceScenario struct { |
| 29 | + sdClient cloudmap.ServiceDiscoveryClient |
| 30 | + expectedSvc model.Service |
| 31 | +} |
| 32 | + |
| 33 | +func NewExportServiceScenario(cfg *aws.Config, nsName string, svcName string, portStr string, ips string) (ExportServiceScenario, error) { |
| 34 | + endpts := make([]*model.Endpoint, 0) |
| 35 | + |
| 36 | + port, parseError := strconv.ParseUint(portStr, 10, 16) |
| 37 | + if parseError != nil { |
| 38 | + return nil, parseError |
| 39 | + } |
| 40 | + |
| 41 | + for _, ip := range strings.Split(ips, ",") { |
| 42 | + endpts = append(endpts, &model.Endpoint{ |
| 43 | + Id: model.EndpointIdFromIPAddress(ip), |
| 44 | + IP: ip, |
| 45 | + Port: int32(port), |
| 46 | + Attributes: make(map[string]string, 0), |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + return &exportServiceScenario{ |
| 51 | + sdClient: cloudmap.NewServiceDiscoveryClientWithCustomCache(cfg, |
| 52 | + &cloudmap.SdCacheConfig{ |
| 53 | + NsTTL: time.Second, |
| 54 | + SvcTTL: time.Second, |
| 55 | + EndptTTL: time.Second, |
| 56 | + }), |
| 57 | + expectedSvc: model.Service{ |
| 58 | + Namespace: nsName, |
| 59 | + Name: svcName, |
| 60 | + Endpoints: endpts, |
| 61 | + }, |
| 62 | + }, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (e *exportServiceScenario) Run() error { |
| 66 | + fmt.Printf("Seeking expected service: %v\n", e.expectedSvc) |
| 67 | + |
| 68 | + return wait.Poll(defaultScenarioPollInterval, defaultScenarioPollTimeout, func() (done bool, err error) { |
| 69 | + fmt.Println("Polling service...") |
| 70 | + cmSvc, err := e.sdClient.GetService(context.TODO(), e.expectedSvc.Namespace, e.expectedSvc.Name) |
| 71 | + if err != nil { |
| 72 | + return true, err |
| 73 | + } |
| 74 | + |
| 75 | + if cmSvc == nil { |
| 76 | + fmt.Println("Service not found.") |
| 77 | + return false, nil |
| 78 | + } |
| 79 | + |
| 80 | + fmt.Printf("Found service: %v\n", cmSvc) |
| 81 | + return e.compareEndpoints(cmSvc.Endpoints), nil |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func (e *exportServiceScenario) compareEndpoints(cmEndpoints []*model.Endpoint) bool { |
| 86 | + if len(e.expectedSvc.Endpoints) != len(cmEndpoints) { |
| 87 | + fmt.Println("Endpoints do not match.") |
| 88 | + return false |
| 89 | + } |
| 90 | + |
| 91 | + for _, expected := range e.expectedSvc.Endpoints { |
| 92 | + match := false |
| 93 | + for _, actual := range cmEndpoints { |
| 94 | + // Ignore K8S instance attribute for the purpose of this test. |
| 95 | + delete(actual.Attributes, controllers.K8sVersionAttr) |
| 96 | + if expected.Equals(actual) { |
| 97 | + match = true |
| 98 | + break |
| 99 | + } |
| 100 | + } |
| 101 | + if !match { |
| 102 | + fmt.Println("Endpoints do not match.") |
| 103 | + return false |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + fmt.Println("Endpoints match.") |
| 108 | + return true |
| 109 | +} |
0 commit comments