Files
kubevela/references/appfile/addon_test.go
AshvinBambhaniya2003 10b45d3a8f Fix(references/appfile): Fix namespace check and Terraform output parsing (#6915)
* fix(references/appfile): correct namespace existence check in addon

The `generateSecretFromTerraformOutput` function was using an incorrect logic to check for namespace existence. It was trying to create the namespace and if it succeeded, it would return an error.

This commit corrects the logic to use `k8sClient.Get` and checks for a `NotFound` error to accurately determine if the namespace exists.

Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>

* fix(references/appfile): make terraform output parsing robust

The previous implementation for parsing `terraform output` was fragile and could lead to data corruption or errors. It would incorrectly remove all spaces from values and would fail to parse values that contained an equals sign.

This commit refactors the parsing logic to be more robust:
- It no longer removes spaces from output values, preserving them correctly.
- It correctly parses `key=value` pairs by splitting only on the first equals sign in a line.
- It properly handles quoted string values from Terraform.

The corresponding tests in `addon_test.go` have been updated to align with the refactored function signature and verify the new, robust behavior.

Fixes #6916

Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>

---------

Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>
2025-10-06 11:57:37 -07:00

73 lines
2.4 KiB
Go

/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package appfile
import (
"fmt"
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
commontype "github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/utils/common"
"github.com/oam-dev/kubevela/pkg/utils/util"
)
var _ = It("Test ApplyTerraform", func() {
app := &v1beta1.Application{
ObjectMeta: v1.ObjectMeta{Name: "test-terraform-app"},
Spec: v1beta1.ApplicationSpec{Components: []commontype.ApplicationComponent{{
Name: "test-terraform-svc",
Type: "aliyun-oss",
Properties: &runtime.RawExtension{Raw: []byte("{\"bucket\": \"oam-website\"}")},
},
}},
}
ioStream := util.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
arg := common.Args{
Schema: scheme,
}
err := arg.SetConfig(cfg)
Expect(err).Should(BeNil())
_, err = ApplyTerraform(app, k8sClient, ioStream, addonNamespace, arg)
Expect(err).Should(BeNil())
})
var _ = Describe("Test generateSecretFromTerraformOutput", func() {
var name = "test-addon-secret"
It("namespace doesn't exist", func() {
badNamespace := "a-not-existed-namespace"
err := generateSecretFromTerraformOutput(k8sClient, "", name, badNamespace)
Expect(err).Should(Equal(fmt.Errorf("namespace %s doesn't exist", badNamespace)))
})
It("valid output list", func() {
rawOutput := "name=aaa\nage=1"
err := generateSecretFromTerraformOutput(k8sClient, rawOutput, name, addonNamespace)
Expect(err).Should(BeNil())
})
It("invalid output list", func() {
rawOutput := "name"
err := generateSecretFromTerraformOutput(k8sClient, rawOutput, name, addonNamespace)
Expect(err).Should(Equal(fmt.Errorf("terraform output isn't in the right format: %q", rawOutput)))
})
})