<commit_before>package integration_test

import (
	"fmt"
	"os/exec"
	"path/filepath"
	"testing"

	"github.com/cloudfoundry/libbuildpack/cutlass"
	"github.com/sclevine/spec"

	. "github.com/onsi/gomega"
)

func testDynatrace(t *testing.T, context spec.G, it spec.S) {
	var (
		Expect = NewWithT(t).Expect

		app      *cutlass.App
		services []string
	)

	it.Before(func() {
		app = cutlass.New(filepath.Join(settings.FixturesPath, "default", "simple"))
		app.SetEnv("BP_DEBUG", "true")
		PushAppAndConfirm(t, app)
	})

	it.After(func() {
		app = DestroyApp(t, app)

		for _, service := range services {
			command := exec.Command("cf", "delete-service", "-f", service)
			_, err := command.Output()
			Expect(err).NotTo(HaveOccurred())
		}
	})

	context("when deploying with Dynatrace agent with single credentials service", func() {
		it("checks if Dynatrace injection was successful", func() {
			service := "dynatrace-" + cutlass.RandStringRunes(20) + "-service"
			command := exec.Command("cf", "cups", service, "-p", fmt.Sprintf("'{\"apitoken\":\"se<PASSWORD>astoken\",\"apiurl\":\"%s\",\"environmentid\":\"envid\"}'", settings.Dynatrace.URI))
			output, err := command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			services = append(services, service)

			command = exec.Command("cf", "bind-service", app.Name, service)
			output, err = command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			command = exec.Command("cf", "restage", app.Name)
			output, err = command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			Expect(app.ConfirmBuildpack(settings.Buildpack.Version)).To(Succeed())
			Expect(app.Stdout.String()).To(ContainSubstring("Dynatrace service credentials found. Setting up Dynatrace OneAgent."))
			Expect(app.Stdout.String()).To(ContainSubstring("Starting Dynatrace OneAgent installer"))
			Expect(app.Stdout.String()).To(ContainSubstring("Copy dynatrace-env.sh"))
			Expect(app.Stdout.String()).To(ContainSubstring("Dynatrace OneAgent installed."))
			Expect(app.Stdout.String()).To(ContainSubstring("Dynatrace OneAgent injection is set up."))
		})
	})

	context("when deploying with Dynatrace agent with two credentials services", func() {
		it("checks if detection of second service with credentials works", func() {
			service := "dynatrace-" + cutlass.RandStringRunes(20) + "-service"
			command := exec.Command("cf", "cups", service, "-p", fmt.Sprintf("'{\"apitoken\":\"<PASSWORD>\",\"apiurl\":\"%s\",\"environmentid\":\"envid\"}'", settings.Dynatrace.URI))
			output, err := command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			services = append(services, service)

			command = exec.Command("cf", "bind-service", app.Name, service)
			output, err = command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			service = "dynatrace-dupe-" + cutlass.RandStringRunes(20) + "-service"
			command = exec.Command("cf", "cups", service, "-p", fmt.Sprintf("'{\"apitoken\":\"<PASSWORD>\",\"apiurl\":\"%s\",\"environmentid\":\"envid\"}'", settings.Dynatrace.URI))
			output, err = command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			services = append(services, service)

			command = exec.Command("cf", "bind-service", app.Name, service)
			output, err = command.CombinedOutput()<commit_msg>Move dynatrace network zone test to spec<commit_after>package integration_test

import (
	"fmt"
	"os/exec"
	"path/filepath"
	"testing"

	"github.com/cloudfoundry/libbuildpack/cutlass"
	"github.com/sclevine/spec"

	. "github.com/onsi/gomega"
)

func testDynatrace(t *testing.T, context spec.G, it spec.S) {
	var (
		Expect = NewWithT(t).Expect

		app      *cutlass.App
		services []string
	)

	it.Before(func() {
		app = cutlass.New(filepath.Join(settings.FixturesPath, "default", "simple"))
		app.SetEnv("BP_DEBUG", "true")
		PushAppAndConfirm(t, app)
	})

	it.After(func() {
		app = DestroyApp(t, app)

		for _, service := range services {
			command := exec.Command("cf", "delete-service", "-f", service)
			_, err := command.Output()
			Expect(err).NotTo(HaveOccurred())
		}
	})

	context("when deploying with Dynatrace agent with single credentials service", func() {
		it("checks if Dynatrace injection was successful", func() {
			service := "dynatrace-" + cutlass.RandStringRunes(20) + "-service"
			command := exec.Command("cf", "cups", service, "-p", fmt.Sprintf("'{\"apitoken\":\"<PASSWORD>\",\"apiurl\":\"%s\",\"environmentid\":\"envid\"}'", settings.Dynatrace.URI))
			output, err := command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			services = append(services, service)

			command = exec.Command("cf", "bind-service", app.Name, service)
			output, err = command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			command = exec.Command("cf", "restage", app.Name)
			output, err = command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			Expect(app.ConfirmBuildpack(settings.Buildpack.Version)).To(Succeed())
			Expect(app.Stdout.String()).To(ContainSubstring("Dynatrace service credentials found. Setting up Dynatrace OneAgent."))
			Expect(app.Stdout.String()).To(ContainSubstring("Starting Dynatrace OneAgent installer"))
			Expect(app.Stdout.String()).To(ContainSubstring("Copy dynatrace-env.sh"))
			Expect(app.Stdout.String()).To(ContainSubstring("Dynatrace OneAgent installed."))
			Expect(app.Stdout.String()).To(ContainSubstring("Dynatrace OneAgent injection is set up."))
		})
	})

	context("deploying a Go app with Dynatrace agent with configured network zone", func() {
		it("checks if networkzone setting was successful", func() {
			serviceName := "dynatrace-" + cutlass.RandStringRunes(20) + "-service"
			command := exec.Command("cf", "cups", serviceName, "-p", fmt.Sprintf("'{\"apitoken\":\"<PASSWORD>\",\"apiurl\":\"%s\",\"environmentid\":\"envid\", \"networkzone\":\"testzone\"}'", settings.Dynatrace.URI))
			_, err := command.CombinedOutput()
			Expect(err).To(BeNil())
			services = append(services, serviceName)

			command = exec.Command("cf", "bind-service", app.Name, serviceName)
			_, err = command.CombinedOutput()
			Expect(err).To(BeNil())
			command = exec.Command("cf", "restage", app.Name)
			_, err = command.Output()
			Expect(err).To(BeNil())

			Expect(app.ConfirmBuildpack(settings.Buildpack.Version)).To(Succeed())
			Expect(app.Stdout.String()).To(ContainSubstring("Dynatrace service credentials found. Setting up Dynatrace OneAgent."))
			Expect(app.Stdout.String()).To(ContainSubstring("Starting Dynatrace OneAgent installer"))
			Expect(app.Stdout.String()).To(ContainSubstring("Copy dynatrace-env.sh"))
			Expect(app.Stdout.String()).To(ContainSubstring("Setting DT_NETWORK_ZONE..."))
			Expect(app.Stdout.String()).To(ContainSubstring("Dynatrace OneAgent installed."))
			Expect(app.Stdout.String()).To(ContainSubstring("Dynatrace OneAgent injection is set up."))
		})
	})

	context("when deploying with Dynatrace agent with two credentials services", func() {
		it("checks if detection of second service with credentials works", func() {
			service := "dynatrace-" + cutlass.RandStringRunes(20) + "-service"
			command := exec.Command("cf", "cups", service, "-p", fmt.Sprintf("'{\"apitoken\":\"<PASSWORD>\",\"apiurl\":\"%s\",\"environmentid\":\"envid\"}'", settings.Dynatrace.URI))
			output, err := command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			services = append(services, service)

			command = exec.Command("cf", "bind-service", app.Name, service)
			output, err = command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			service = "dynatrace-dupe-" + cutlass.RandStringRunes(20) + "-service"
			command = exec.Command("cf", "cups", service, "-p", fmt.Sprintf("'{\"apitoken\":\"<PASSWORD>\",\"apiurl\":\"%s\",\"environmentid\":\"envid\"}'", settings.Dynatrace.URI))
			output, err = command.CombinedOutput()
			Expect(err).NotTo(HaveOccurred(), string(output))

			services = append(services, service)

			command = exec.Command("cf", "bind-service", app.Name, service)
			output, err = command.CombinedOutput()
{'fraction_non_alphanumeric': 0.18389540566959922, 'fraction_numerical': 0.001710654936461388, 'mean_word_length': 5.03168754605748, 'pattern_counts': {'":': 22, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '913788', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2655, 'n_words': 491}