github.com/in4it/ecs-deploy@v0.0.42-0.20240508120354-ed77ff16df25/provider/ecs/ecs_test.go (about)

     1  package ecs
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/ghodss/yaml"
    12  
    13  	"github.com/in4it/ecs-deploy/service"
    14  	"github.com/in4it/ecs-deploy/util"
    15  )
    16  
    17  func initDeployment() (service.DeployServices, error) {
    18  	var (
    19  		d service.DeployServices
    20  	)
    21  	dat, err := ioutil.ReadFile("testdata/ecs.yaml")
    22  	if err != nil {
    23  		return d, err
    24  	}
    25  
    26  	err = yaml.Unmarshal(dat, &d)
    27  	if err != nil {
    28  		return d, err
    29  	}
    30  
    31  	if len(d.Services) == 0 {
    32  		return d, fmt.Errorf("No services found in yaml")
    33  	}
    34  
    35  	return d, nil
    36  }
    37  
    38  func TestGetNetworkConfiguration(t *testing.T) {
    39  	var (
    40  		err error
    41  	)
    42  
    43  	d, err := initDeployment()
    44  	if err != nil {
    45  		t.Errorf("initDeployment failed: %s", err)
    46  		return
    47  	}
    48  
    49  	ecs := ECS{}
    50  	networkConfiguration := ecs.getNetworkConfiguration(d.Services[0])
    51  
    52  	if *networkConfiguration.AwsvpcConfiguration.AssignPublicIp != "DISABLED" {
    53  		t.Errorf("Incorrect value for assign public ip: %s", *networkConfiguration.AwsvpcConfiguration.AssignPublicIp)
    54  		return
    55  	}
    56  
    57  	if len(networkConfiguration.AwsvpcConfiguration.SecurityGroups) == 0 {
    58  		t.Errorf("No security groups found")
    59  		return
    60  	}
    61  
    62  	if *networkConfiguration.AwsvpcConfiguration.SecurityGroups[0] != "sg-0123456abc" {
    63  		t.Errorf("Wrong security group")
    64  		return
    65  	}
    66  
    67  	if len(networkConfiguration.AwsvpcConfiguration.Subnets) == 0 {
    68  		t.Errorf("No subnets found")
    69  		return
    70  	}
    71  
    72  	if *networkConfiguration.AwsvpcConfiguration.Subnets[0] != "subnet-0123456abc" {
    73  		t.Errorf("Wrong security group")
    74  		return
    75  	}
    76  }
    77  func TestCreateTaskDefinition(t *testing.T) {
    78  	var (
    79  		secrets map[string]string
    80  		err     error
    81  	)
    82  
    83  	d, err := initDeployment()
    84  	if err != nil {
    85  		t.Errorf("initDeployment failed: %s", err)
    86  		return
    87  	}
    88  
    89  	ecs := ECS{}
    90  	err = ecs.CreateTaskDefinitionInput(d.Services[0], secrets, "0123456789")
    91  	if err != nil {
    92  		t.Errorf("Error: %s", err)
    93  	}
    94  
    95  	// checks
    96  	if len(ecs.TaskDefinition.ContainerDefinitions) == 0 {
    97  		t.Errorf("No container definition found")
    98  	}
    99  
   100  	if *ecs.TaskDefinition.ContainerDefinitions[0].Name != "demo" {
   101  		t.Errorf("Incorrect container definition: name is not demo (got: %s)", *ecs.TaskDefinition.ContainerDefinitions[0].Name)
   102  		return
   103  	}
   104  	if ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration == nil {
   105  		t.Errorf("Incorrect container definition: no logdriver configured")
   106  		return
   107  	}
   108  	if *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.LogDriver != "json-file" {
   109  		t.Errorf("Incorrect container definition: incorrect log driver (got: %s)", *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.LogDriver)
   110  		return
   111  	}
   112  	if val := ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.Options["max-size"]; val == nil {
   113  		t.Errorf("Incorrect container definition: missign max-size log option")
   114  		return
   115  	}
   116  	if *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.Options["max-size"] != "20m" {
   117  		t.Errorf("Incorrect container definition: incorrect log option (got: %s)", *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.Options["max-size"])
   118  		return
   119  	}
   120  	if *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.Options["max-file"] != "1" {
   121  		t.Errorf("Incorrect container definition: incorrect log option (got: %s)", *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.Options["max-file"])
   122  		return
   123  	}
   124  
   125  }
   126  
   127  func TestWaitUntilServicesStable(t *testing.T) {
   128  	if accountId == nil {
   129  		t.Skip(noAWSMsg)
   130  	}
   131  	ecs := ECS{}
   132  	err := ecs.WaitUntilServicesStable(util.GetEnv("TEST_CLUSTERNAME", "test-cluster"), util.GetEnv("TEST_SERVICENAME", "ecs-deploy"), 10)
   133  	if err != nil {
   134  		t.Errorf("Error: %v", err)
   135  		return
   136  	}
   137  }
   138  func TestEcsTaskMetadata(t *testing.T) {
   139  	input := `{
   140    "Cluster": "default",
   141    "TaskARN": "arn:aws:ecs:us-west-2:012345678910:task/9781c248-0edd-4cdb-9a93-f63cb662a5d3",
   142    "Family": "nginx",
   143    "Revision": "5",
   144    "DesiredStatus": "RUNNING",
   145    "KnownStatus": "RUNNING",
   146    "Containers": [
   147      {
   148        "DockerId": "731a0d6a3b4210e2448339bc7015aaa79bfe4fa256384f4102db86ef94cbbc4c",
   149        "Name": "~internal~ecs~pause",
   150        "DockerName": "ecs-nginx-5-internalecspause-acc699c0cbf2d6d11700",
   151        "Image": "amazon/amazon-ecs-pause:0.1.0",
   152        "ImageID": "",
   153        "Labels": {
   154          "com.amazonaws.ecs.cluster": "default",
   155          "com.amazonaws.ecs.container-name": "~internal~ecs~pause",
   156          "com.amazonaws.ecs.task-arn": "arn:aws:ecs:us-west-2:012345678910:task/9781c248-0edd-4cdb-9a93-f63cb662a5d3",
   157          "com.amazonaws.ecs.task-definition-family": "nginx",
   158          "com.amazonaws.ecs.task-definition-version": "5"
   159        },
   160        "DesiredStatus": "RESOURCES_PROVISIONED",
   161        "KnownStatus": "RESOURCES_PROVISIONED",
   162        "Limits": {
   163          "CPU": 0,
   164          "Memory": 0
   165        },
   166        "CreatedAt": "2018-02-01T20:55:08.366329616Z",
   167        "StartedAt": "2018-02-01T20:55:09.058354915Z",
   168        "Type": "CNI_PAUSE",
   169        "Networks": [
   170          {
   171            "NetworkMode": "awsvpc",
   172            "IPv4Addresses": [
   173              "10.0.2.106"
   174            ]
   175          }
   176        ]
   177      },
   178      {
   179        "DockerId": "43481a6ce4842eec8fe72fc28500c6b52edcc0917f105b83379f88cac1ff3946",
   180        "Name": "nginx-curl",
   181        "DockerName": "ecs-nginx-5-nginx-curl-ccccb9f49db0dfe0d901",
   182        "Image": "nrdlngr/nginx-curl",
   183        "ImageID": "sha256:2e00ae64383cfc865ba0a2ba37f61b50a120d2d9378559dcd458dc0de47bc165",
   184        "Labels": {
   185          "com.amazonaws.ecs.cluster": "default",
   186          "com.amazonaws.ecs.container-name": "nginx-curl",
   187          "com.amazonaws.ecs.task-arn": "arn:aws:ecs:us-west-2:012345678910:task/9781c248-0edd-4cdb-9a93-f63cb662a5d3",
   188          "com.amazonaws.ecs.task-definition-family": "nginx",
   189          "com.amazonaws.ecs.task-definition-version": "5"
   190        },
   191        "DesiredStatus": "RUNNING",
   192        "KnownStatus": "RUNNING",
   193        "Limits": {
   194          "CPU": 512,
   195          "Memory": 512
   196        },
   197        "CreatedAt": "2018-02-01T20:55:10.554941919Z",
   198        "StartedAt": "2018-02-01T20:55:11.064236631Z",
   199        "Type": "NORMAL",
   200        "Networks": [
   201          {
   202            "NetworkMode": "awsvpc",
   203            "IPv4Addresses": [
   204              "10.0.2.106"
   205            ]
   206          }
   207        ]
   208      }
   209    ],
   210    "PullStartedAt": "2018-02-01T20:55:09.372495529Z",
   211    "PullStoppedAt": "2018-02-01T20:55:10.552018345Z"
   212  }`
   213  	var task EcsTaskMetadata
   214  	err := json.Unmarshal([]byte(input), &task)
   215  	if err != nil {
   216  		t.Errorf("Error: %v", err)
   217  	}
   218  	split := strings.Split(task.TaskARN, "task/")
   219  	if len(split) != 2 {
   220  		t.Errorf("Error: %v", err)
   221  	}
   222  	if len(split[1]) == 0 {
   223  		t.Errorf("Error: %v", err)
   224  	}
   225  }
   226  
   227  func TestGetMaxWaitMinutes(t *testing.T) {
   228  	ecs := ECS{}
   229  	if num := ecs.getMaxWaitMinutes(0); num != 15 {
   230  		t.Errorf("Got wrong maxWaitMinutes: %d", num)
   231  	}
   232  	if num := ecs.getMaxWaitMinutes(100); num != 20 {
   233  		t.Errorf("Got wrong maxWaitMinutes: %d", num)
   234  	}
   235  	os.Setenv("DEPLOY_MAX_WAIT_SECONDS", "1800")
   236  	if num := ecs.getMaxWaitMinutes(0); num != 30 {
   237  		t.Errorf("Got wrong maxWaitMinutes: %d", num)
   238  	}
   239  	if num := ecs.getMaxWaitMinutes(300); num != 30 {
   240  		t.Errorf("Got wrong maxWaitMinutes: %d", num)
   241  	}
   242  }