본문 바로가기
개발/Terraform on NaverCloud

오픈 소스 기여하기 : terraform-provider-ncloud 이슈 선정

by 컴쏘 2023. 8. 1.

컨트리뷰션에 기여하고 싶은 이슈를 선정하였다.

 

AccTest: Classic loadbalancer test failure · Issue #296 · NaverCloudPlatform/terraform-provider-ncloud

Community Note Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request Please do not leave "+1" or other comments that d...

github.com

 

이슈 내용 파악하기

AccTest: Classic loadbalancer test failure 

이슈의 내용은 Classic loadbalancer test 실패이다.

 

Terraform CLI and Terraform Ncloud Provider Version

Terraform 버전과 Ncloud Provider Version은 다음과 같다. 

Terraform version : v1.5.2
Ncloud Provider version : main(2.3.18)

 

Affected Resource(s)

ncloud_load_balancer

영향을 받는 Resource는 ncloud_load_balancer이다. 

 

Terraform Configuration Files

Please include all Terraform configurations required to reproduce the bug. Bug reports without a functional reproduction may be closed without investigation.

버그를 재현하는 데 필요한 모든 terraform 구성을 포함해야 한다.

기능 재현을 꼭 해야 한다.

 

Debug Output 

--- FAIL: TestAccNcloudLoadBalancerSSLCertificateBasic (0.14s)
    resource_ncloud_load_balancer_ssl_certificate_test.go:81: Step 1/2 error: Error running pre-apply refresh: exit status 1

        Error: Insufficient rule_list blocks

          on terraform_plugin_test.tf line 9, in resource "ncloud_load_balancer" "lb":
           9: 		resource "ncloud_load_balancer" "lb" {

        At least 1 "rule_list" blocks are required.

        Error: Unsupported argument

          on terraform_plugin_test.tf line 14, in resource "ncloud_load_balancer" "lb":
          14: 			rule_list = [

        An argument named "rule_list" is not expected here. Did you mean to define a
        block of type "rule_list"?
FAIL
FAIL	github.com/terraform-providers/terraform-provider-ncloud/ncloud	0.680s
FAIL

 

Expected Behavior

Pass the test

 

Actual Behavior

Test failure 

 

Steps to Reproduce

이 순서대로 bash에 입력해주면 된다. 

export NCLOUD_REGION=KR
export NCLOUD_ACCESS_KEY=xxx
export NCLOUD_SECRET_KEY=xxx
TF_ACC=1 go test ./internal/service/classicloadbalancer/... -run=TestAccNcloudLoadBalancerSSLCertificateBasic -v

 

명령어 뜯어보기

TF_ACC=1 go test ./internal/service/classicloadbalancer/... -run=TestAccNcloudLoadBalancerSSLCertificateBasic -v

TF_ACC=1 : 이는 Terraform에게 Acceptance Test 환경을 활성화하도록 지시하는 환경 변수, Acceptance Test 환경이 활성화되면 테스트 시 실제 클라우드 리소스를 사용함

./internal/service/classicloadbalancer/...: 테스트할 Go 패키지의 경로를 지정

-run=TestAccNcloudLoadBalancerSSLCertificateBasic : 테스트 중 실행할 특정 테스트 함수를 지정

-v : 테스트 실행 로그를 자세히 출력하는 옵션

 

결과 확인 해보기

우선 다음 명령으로 output을 확인했다.

TF_ACC=1 go test load_balancer_ssl_certificate_test.go -run=TestAccNcloudLoadBalancerSSLCertificateBasic -v

실행 결과

결과 뜯어보기

load_balancer_ssl_certificate_test.go:85 : load_balancer_ssl_certificate_test.go 파일의 85번째 줄에서 발생

Step 1/2 error : Step 1/2 error 2개의 단계 중 첫 번째 단계에서 문제 발생

Error running pre-apply refresh: exit status 1 : pre-apply refresh 단계에서 오류가 발생

(pre-apply refresh : 테스트 직전에 Terraform이 실행되어 실제 클라우드 리소스를 생성하기 전에 상태를 업데이트하는 단계)

 

Error: Insufficient rule_list blocks : ncloud_load_balancer 리소스 block에서 rule_list block이 적어도 하나 이상 필요

Error: Unsupported argument : ncloud_load_balancer 리소스 block에서 rule_list라는 argument가 지원되지 않음. block 형태로 제공해야 할 것 같음

 

생각되는 해결 방안

ncloud_load_balancer 리소스 block을 수정하여 rule_list block을 추가한다. 이때, argument의 형태가 아닌 block 형태로 지정해준다.

 

시도 1 - argument type에서 block type으로

 

문제가 있는 부분 살펴보기

 

문서 참고

 

Terraform Registry

 

registry.terraform.io

 

문제가 있는 test 함수 

func testAccLoadBalancerSSLCertificateConfig(certificateName string, privatekey string, publickeyCertificate string, lbName string) string {
	return fmt.Sprintf(`
		resource "ncloud_load_balancer_ssl_certificate" "cert" {
			certificate_name      = "%s"
			privatekey            = "%s"
			publickey_certificate = "%s"
		}

		resource "ncloud_load_balancer" "lb" {
			name           = "%s"
			algorithm_type = "SIPHS"
			description    = "tftest_lb description"

			rule_list = [
				{
					protocol_type        = "HTTP"
					load_balancer_port   = 80
					server_port          = 80
					l7_health_check_path = "/monitor/l7check"
				},
				{
					protocol_type        = "HTTPS"
					load_balancer_port   = 443
					server_port          = 443
					l7_health_check_path = "/monitor/l7check"
					certificate_name     = "${ncloud_load_balancer_ssl_certificate.cert.certificate_name}"
				},
			]

			region = "KR"
		}
		`, certificateName, strings.Replace(privatekey, "\\n", "\\\\n", -1), strings.Replace(publickeyCertificate, "\\n", "\\\\n", -1), lbName)
}

 

문제가 되는 부분

rule_list = [
				{
					protocol_type        = "HTTP"
					load_balancer_port   = 80
					server_port          = 80
					l7_health_check_path = "/monitor/l7check"
				},
				{
					protocol_type        = "HTTPS"
					load_balancer_port   = 443
					server_port          = 443
					l7_health_check_path = "/monitor/l7check"
					certificate_name     = "${ncloud_load_balancer_ssl_certificate.cert.certificate_name}"
				},
			]

 

argument가 아닌 block 형태로 바꿔주기

rule_list {
	protocol_type        = "HTTP"
	load_balancer_port   = 80
	server_port          = 80
	l7_health_check_path = "/monitor/l7check"
}

rule_list {
	protocol_type        = "HTTPS"
	load_balancer_port   = 443
	server_port          = 443
	l7_health_check_path = "/monitor/l7check"
	certificate_name     = "${ncloud_load_balancer_ssl_certificate.cert.certificate_name}"
}

 

test 함수에 적용해보기

func testAccLoadBalancerSSLCertificateConfig(certificateName string, privatekey string, publickeyCertificate string, lbName string) string {
	return fmt.Sprintf(`
		resource "ncloud_load_balancer_ssl_certificate" "cert" {
			certificate_name      = "%s"
			privatekey            = "%s"
			publickey_certificate = "%s"
		}

		resource "ncloud_load_balancer" "lb" {
			name           = "%s"
			algorithm_type = "SIPHS"
			description    = "tftest_lb description"

			rule_list {
				protocol_type        = "HTTP"
				load_balancer_port   = 80
				server_port          = 80
				l7_health_check_path = "/monitor/l7check"
			}
			
			rule_list {
				protocol_type        = "HTTPS"
				load_balancer_port   = 443
				server_port          = 443
				l7_health_check_path = "/monitor/l7check"
				certificate_name     = "${ncloud_load_balancer_ssl_certificate.cert.certificate_name}"
			}

			region = "KR"
		}
		`, certificateName, strings.Replace(privatekey, "\\n", "\\\\n", -1), strings.Replace(publickeyCertificate, "\\n", "\\\\n", -1), lbName)
}

 

다시 실행해보자 

 

FAIL

결과.. 실패...

panic 발생...! 

=== RUN   TestAccNcloudLoadBalancerSSLCertificateBasic
panic: runtime error: index out of range [0] with length 0

goroutine 233 [running]:
github.com/terraform-providers/terraform-provider-ncloud/internal/service/classicloadbalancer.resourceNcloudLoadBalancerSSLCertificateCreate(0x0?, {0x159ce20?, 0xc000570740})
        C:/Users/jpark/Desktop/terraform_sohyun/terraform-provider-ncloud/internal/service/classicloadbalancer/load_balancer_ssl_certificate.go:76 +0x205
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0x18ff940?, {0x18ff940?, 0xc000722390?}, 0xd?, {0x159ce20?, 0xc000570740?})
        C:/Users/jpark/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.1/helper/schema/resource.go:695 +0x178
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0xc00048cd20, {0x18ff940, 0xc000722390}, 0xc0009bc4e0, 0xc0006bdc00, {0x159ce20, 0xc000570740})
        C:/Users/jpark/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.1/helper/schema/resource.go:837 +0xa85
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0xc0000080d8, {0x18ff940?, 0xc0007222d0?}, 0xc000544960)
        C:/Users/jpark/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.1/helper/schema/grpc_provider.go:1021 +0xe8d
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0xc000472be0, {0x18ff940?, 0xc00070faa0?}, 0xc000190d20)
        C:/Users/jpark/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.14.1/tfprotov5/tf5server/server.go:818 +0x574
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x172bda0?, 0xc000472be0}, {0x18ff940, 0xc00070faa0}, 0xc000190cb0, 0x0)   
        C:/Users/jpark/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.14.1/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:385 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0004a65a0, {0x1903718, 0xc00008cea0}, 0xc00083c360, 0xc00073e8d0, 0x1f5d6a0, 0x0)
        C:/Users/jpark/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:1336 +0xd33
google.golang.org/grpc.(*Server).handleStream(0xc0004a65a0, {0x1903718, 0xc00008cea0}, 0xc00083c360, 0x0)
        C:/Users/jpark/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:1704 +0xa36
google.golang.org/grpc.(*Server).serveStreams.func1.2()
        C:/Users/jpark/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:965 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
        C:/Users/jpark/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:963 +0x28a
FAIL    command-line-arguments  1.804s
FAIL

 

to be continue...