github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/rules/awsrules/aws_launch_configuration_invalid_image_id_test.go (about)

     1  package awsrules
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/golang/mock/gomock"
    11  	hcl "github.com/hashicorp/hcl/v2"
    12  	"github.com/wata727/tflint/client"
    13  	"github.com/wata727/tflint/tflint"
    14  )
    15  
    16  func Test_AwsLaunchConfigurationInvalidImageID_invalid(t *testing.T) {
    17  	content := `
    18  resource "aws_launch_configuration" "invalid" {
    19  	image_id = "ami-1234abcd"
    20  }`
    21  	runner := tflint.TestRunner(t, map[string]string{"instances.tf": content})
    22  
    23  	ctrl := gomock.NewController(t)
    24  	defer ctrl.Finish()
    25  
    26  	ec2mock := client.NewMockEC2API(ctrl)
    27  	ec2mock.EXPECT().DescribeImages(&ec2.DescribeImagesInput{
    28  		ImageIds: aws.StringSlice([]string{"ami-1234abcd"}),
    29  	}).Return(&ec2.DescribeImagesOutput{
    30  		Images: []*ec2.Image{},
    31  	}, nil)
    32  	runner.AwsClient.EC2 = ec2mock
    33  
    34  	rule := NewAwsLaunchConfigurationInvalidImageIDRule()
    35  	if err := rule.Check(runner); err != nil {
    36  		t.Fatalf("Unexpected error occurred: %s", err)
    37  	}
    38  
    39  	expected := tflint.Issues{
    40  		{
    41  			Rule:    NewAwsLaunchConfigurationInvalidImageIDRule(),
    42  			Message: "\"ami-1234abcd\" is invalid image ID.",
    43  			Range: hcl.Range{
    44  				Filename: "instances.tf",
    45  				Start:    hcl.Pos{Line: 3, Column: 13},
    46  				End:      hcl.Pos{Line: 3, Column: 27},
    47  			},
    48  		},
    49  	}
    50  
    51  	tflint.AssertIssues(t, expected, runner.Issues)
    52  }
    53  
    54  func Test_AwsLaunchConfigurationInvalidImageID_valid(t *testing.T) {
    55  	content := `
    56  resource "aws_launch_configuration" "valid" {
    57  	image_id = "ami-9ad76sd1"
    58  }`
    59  	runner := tflint.TestRunner(t, map[string]string{"instances.tf": content})
    60  
    61  	ctrl := gomock.NewController(t)
    62  	defer ctrl.Finish()
    63  
    64  	ec2mock := client.NewMockEC2API(ctrl)
    65  	ec2mock.EXPECT().DescribeImages(&ec2.DescribeImagesInput{
    66  		ImageIds: aws.StringSlice([]string{"ami-9ad76sd1"}),
    67  	}).Return(&ec2.DescribeImagesOutput{
    68  		Images: []*ec2.Image{
    69  			{
    70  				ImageId: aws.String("ami-9ad76sd1"),
    71  			},
    72  		},
    73  	}, nil)
    74  	runner.AwsClient.EC2 = ec2mock
    75  
    76  	rule := NewAwsLaunchConfigurationInvalidImageIDRule()
    77  	if err := rule.Check(runner); err != nil {
    78  		t.Fatalf("Unexpected error occurred: %s", err)
    79  	}
    80  
    81  	expected := tflint.Issues{}
    82  	tflint.AssertIssues(t, expected, runner.Issues)
    83  }
    84  
    85  func Test_AwsLaunchConfigurationInvalidImageID_error(t *testing.T) {
    86  	cases := []struct {
    87  		Name     string
    88  		Content  string
    89  		Request  *ec2.DescribeImagesInput
    90  		Response error
    91  		Error    tflint.Error
    92  	}{
    93  		{
    94  			Name: "AWS API error",
    95  			Content: `
    96  resource "aws_launch_configuration" "valid" {
    97    image_id = "ami-9ad76sd1"
    98  }`,
    99  			Request: &ec2.DescribeImagesInput{
   100  				ImageIds: aws.StringSlice([]string{"ami-9ad76sd1"}),
   101  			},
   102  			Response: awserr.New(
   103  				"MissingRegion",
   104  				"could not find region configuration",
   105  				nil,
   106  			),
   107  			Error: tflint.Error{
   108  				Code:    tflint.ExternalAPIError,
   109  				Level:   tflint.ErrorLevel,
   110  				Message: "An error occurred while describing images; MissingRegion: could not find region configuration",
   111  			},
   112  		},
   113  		{
   114  			Name: "Unexpected error",
   115  			Content: `
   116  resource "aws_launch_configuration" "valid" {
   117  	image_id = "ami-9ad76sd1"
   118  }`,
   119  			Request: &ec2.DescribeImagesInput{
   120  				ImageIds: aws.StringSlice([]string{"ami-9ad76sd1"}),
   121  			},
   122  			Response: errors.New("Unexpected"),
   123  			Error: tflint.Error{
   124  				Code:    tflint.ExternalAPIError,
   125  				Level:   tflint.ErrorLevel,
   126  				Message: "An error occurred while describing images; Unexpected",
   127  			},
   128  		},
   129  	}
   130  
   131  	ctrl := gomock.NewController(t)
   132  	defer ctrl.Finish()
   133  
   134  	rule := NewAwsLaunchConfigurationInvalidImageIDRule()
   135  
   136  	for _, tc := range cases {
   137  		runner := tflint.TestRunner(t, map[string]string{"instances.tf": tc.Content})
   138  
   139  		ec2mock := client.NewMockEC2API(ctrl)
   140  		ec2mock.EXPECT().DescribeImages(tc.Request).Return(nil, tc.Response)
   141  		runner.AwsClient.EC2 = ec2mock
   142  
   143  		err := rule.Check(runner)
   144  		tflint.AssertAppError(t, tc.Error, err)
   145  	}
   146  }
   147  
   148  func Test_AwsLaunchConfigurationInvalidImageID_AMIError(t *testing.T) {
   149  	cases := []struct {
   150  		Name     string
   151  		Content  string
   152  		Request  *ec2.DescribeImagesInput
   153  		Response error
   154  		Issues   tflint.Issues
   155  		Error    bool
   156  	}{
   157  		{
   158  			Name: "not found",
   159  			Content: `
   160  resource "aws_launch_configuration" "not_found" {
   161  	image_id = "ami-9ad76sd1"
   162  }`,
   163  			Request: &ec2.DescribeImagesInput{
   164  				ImageIds: aws.StringSlice([]string{"ami-9ad76sd1"}),
   165  			},
   166  			Response: awserr.New(
   167  				"InvalidAMIID.NotFound",
   168  				"The image id '[ami-9ad76sd1]' does not exist",
   169  				nil,
   170  			),
   171  			Issues: tflint.Issues{
   172  				{
   173  					Rule:    NewAwsLaunchConfigurationInvalidImageIDRule(),
   174  					Message: "\"ami-9ad76sd1\" is invalid image ID.",
   175  					Range: hcl.Range{
   176  						Filename: "instances.tf",
   177  						Start:    hcl.Pos{Line: 3, Column: 13},
   178  						End:      hcl.Pos{Line: 3, Column: 27},
   179  					},
   180  				},
   181  			},
   182  			Error: false,
   183  		},
   184  		{
   185  			Name: "malformed",
   186  			Content: `
   187  resource "aws_launch_configuration" "malformed" {
   188  	image_id = "image-9ad76sd1"
   189  }`,
   190  			Request: &ec2.DescribeImagesInput{
   191  				ImageIds: aws.StringSlice([]string{"image-9ad76sd1"}),
   192  			},
   193  			Response: awserr.New(
   194  				"InvalidAMIID.Malformed",
   195  				"Invalid id: \"image-9ad76sd1\" (expecting \"ami-...\")",
   196  				nil,
   197  			),
   198  			Issues: tflint.Issues{
   199  				{
   200  					Rule:    NewAwsLaunchConfigurationInvalidImageIDRule(),
   201  					Message: "\"image-9ad76sd1\" is invalid image ID.",
   202  					Range: hcl.Range{
   203  						Filename: "instances.tf",
   204  						Start:    hcl.Pos{Line: 3, Column: 13},
   205  						End:      hcl.Pos{Line: 3, Column: 29},
   206  					},
   207  				},
   208  			},
   209  			Error: false,
   210  		},
   211  		{
   212  			Name: "unavailable",
   213  			Content: `
   214  resource "aws_launch_configuration" "unavailable" {
   215  	image_id = "ami-1234567"
   216  }`,
   217  			Request: &ec2.DescribeImagesInput{
   218  				ImageIds: aws.StringSlice([]string{"ami-1234567"}),
   219  			},
   220  			Response: awserr.New(
   221  				"InvalidAMIID.Unavailable",
   222  				"The image ID: 'ami-1234567' is no longer available",
   223  				nil,
   224  			),
   225  			Issues: tflint.Issues{
   226  				{
   227  					Rule:    NewAwsLaunchConfigurationInvalidImageIDRule(),
   228  					Message: "\"ami-1234567\" is invalid image ID.",
   229  					Range: hcl.Range{
   230  						Filename: "instances.tf",
   231  						Start:    hcl.Pos{Line: 3, Column: 13},
   232  						End:      hcl.Pos{Line: 3, Column: 26},
   233  					},
   234  				},
   235  			},
   236  			Error: false,
   237  		},
   238  	}
   239  
   240  	ctrl := gomock.NewController(t)
   241  	defer ctrl.Finish()
   242  
   243  	rule := NewAwsLaunchConfigurationInvalidImageIDRule()
   244  
   245  	for _, tc := range cases {
   246  		runner := tflint.TestRunner(t, map[string]string{"instances.tf": tc.Content})
   247  
   248  		ec2mock := client.NewMockEC2API(ctrl)
   249  		ec2mock.EXPECT().DescribeImages(tc.Request).Return(nil, tc.Response)
   250  		runner.AwsClient.EC2 = ec2mock
   251  
   252  		err := rule.Check(runner)
   253  		if err != nil && !tc.Error {
   254  			t.Fatalf("Failed `%s` test: unexpected error occurred: %s", tc.Name, err)
   255  		}
   256  		if err == nil && tc.Error {
   257  			t.Fatalf("Failed `%s` test: expected to return an error, but nothing occurred", tc.Name)
   258  		}
   259  
   260  		tflint.AssertIssues(t, tc.Issues, runner.Issues)
   261  	}
   262  }