github.com/tetratelabs/proxy-wasm-go-sdk@v0.23.1-0.20240517021853-021aa9cf78e8/e2e/main_test.go (about)

     1  // Copyright 2021 Tetrate
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package e2e
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"log"
    21  	"net/http"
    22  	"os"
    23  	"os/exec"
    24  	"strconv"
    25  	"strings"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  func TestMain(m *testing.M) {
    34  	if err := os.Chdir(".."); err != nil {
    35  		log.Fatal(err)
    36  	}
    37  	os.Exit(m.Run())
    38  }
    39  
    40  func checkMessage(str string, exps []string) bool {
    41  	for _, exp := range exps {
    42  		if !strings.Contains(str, exp) {
    43  			return false
    44  		}
    45  	}
    46  	return true
    47  }
    48  
    49  func startEnvoy(t *testing.T, adminPort int) (stdErr *bytes.Buffer, kill func()) {
    50  	name := strings.TrimPrefix(t.Name(), "Test_")
    51  	cmd := exec.Command("envoy",
    52  		"--base-id", strconv.Itoa(adminPort),
    53  		"--concurrency", "1", "--component-log-level", "wasm:trace",
    54  		"-c", fmt.Sprintf("./examples/%s/envoy.yaml", name))
    55  
    56  	buf := new(bytes.Buffer)
    57  	cmd.Stderr = buf
    58  	require.NoError(t, cmd.Start())
    59  	if !assert.Eventually(t, func() bool {
    60  		res, err := http.Get(fmt.Sprintf("http://localhost:%d/listeners", adminPort))
    61  		if err != nil {
    62  			return false
    63  		}
    64  		defer res.Body.Close()
    65  		return res.StatusCode == http.StatusOK
    66  	}, 5*time.Second, 100*time.Millisecond, "Envoy has not started") {
    67  		t.Fatalf("Envoy stderr: %s", buf.String())
    68  	}
    69  	return buf, func() { require.NoError(t, cmd.Process.Kill()) }
    70  }