Skip to content

Commit

Permalink
Add containerapps-albumapi-go test project & cases (#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroFish91 authored Jun 28, 2024
1 parent a1210d4 commit 48c92ef
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 9 deletions.
14 changes: 10 additions & 4 deletions test/nightly/deployWorkspaceProject/dwpTestScenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { generateAlbumApiGolangTestCases } from "./testCases/albumApiGolangTestCases";
import { generateAlbumApiJavaScriptTestCases } from "./testCases/albumApiJavaScriptTestCases";
import { type DeployWorkspaceProjectTestCase } from "./testCases/DeployWorkspaceProjectTestCase";
import { generateMonoRepoBasicTestCases } from "./testCases/monoRepoBasicTestCases";
Expand All @@ -15,13 +16,18 @@ interface DeployWorkspaceProjectTestScenario {

export const dwpTestScenarios: DeployWorkspaceProjectTestScenario[] = [
{
label: 'monorepo',
folderName: 'monorepo-basic',
testCases: generateMonoRepoBasicTestCases()
label: 'albumapi-go',
folderName: 'albumapi-go',
testCases: generateAlbumApiGolangTestCases()
},
{
label: 'albumapi-js',
folderName: 'albumapi-js',
testCases: generateAlbumApiJavaScriptTestCases()
}
},
{
label: 'monorepo',
folderName: 'monorepo-basic',
testCases: generateMonoRepoBasicTestCases()
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { randomUtils } from "@microsoft/vscode-azext-utils";
import * as path from "path";
import { type DeploymentConfigurationSettings } from "../../../../extension.bundle";
import { type StringOrRegExpProps } from "../../../typeUtils";
import { dwpTestUtils } from "../dwpTestUtils";
import { type DeployWorkspaceProjectTestCase } from "./DeployWorkspaceProjectTestCase";

export function generateAlbumApiGolangTestCases(): DeployWorkspaceProjectTestCase[] {
const folderName: string = 'albumapi-go';
const appResourceName: string = 'album-api';
const sharedResourceName: string = appResourceName + randomUtils.getRandomHexString(4);
const acrResourceName: string = sharedResourceName.replace(/[^a-zA-Z0-9]+/g, '');

return [
{
label: 'Deploy App',
inputs: [
new RegExp(folderName, 'i'),
new RegExp('Create new container apps environment', 'i'),
'Continue',
sharedResourceName,
appResourceName,
`.${path.sep}src`,
'East US',
'Save'
],
expectedResults: dwpTestUtils.generateExpectedResults(sharedResourceName, acrResourceName, appResourceName),
expectedVSCodeSettings: {
deploymentConfigurations: [
generateExpectedDeploymentConfiguration(sharedResourceName, acrResourceName, appResourceName)
]
},
postTestAssertion: dwpTestUtils.generatePostTestAssertion({ targetPort: 8080, env: undefined })
},
{
label: 'Re-deploy App',
inputs: [
new RegExp(folderName, 'i'),
appResourceName,
'Continue'
],
expectedResults: dwpTestUtils.generateExpectedResults(sharedResourceName, acrResourceName, appResourceName),
expectedVSCodeSettings: {
deploymentConfigurations: [
generateExpectedDeploymentConfiguration(sharedResourceName, acrResourceName, appResourceName)
]
},
postTestAssertion: dwpTestUtils.generatePostTestAssertion({ targetPort: 8080, env: undefined })
}
];
}

function generateExpectedDeploymentConfiguration(sharedResourceName: string, acrResourceName: string, appResourceName: string): StringOrRegExpProps<DeploymentConfigurationSettings> {
return {
label: appResourceName,
type: 'AcrDockerBuildRequest',
dockerfilePath: path.join('src', 'Dockerfile'),
srcPath: 'src',
envPath: '',
resourceGroup: sharedResourceName,
containerApp: appResourceName,
containerRegistry: new RegExp(`${acrResourceName}.{6}`, 'i'),
};
}
12 changes: 8 additions & 4 deletions test/test.code-workspace
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"folders": [
{
"name": "albumapi-go",
"path": "./testProjects/containerapps-albumapi-go"
},
{
"name": "albumapi-js",
"path": "./testProjects/containerapps-albumapi-javascript"
},
{
"name": "dockerfiles",
"path": "./testProjects/dockerfiles"
Expand All @@ -8,9 +16,5 @@
"name": "monorepo-basic",
"path": "./testProjects/monorepo-basic"
},
{
"name": "albumapi-js",
"path": "./testProjects/containerapps-albumapi-javascript"
},
]
}
93 changes: 93 additions & 0 deletions test/testProjects/containerapps-albumapi-go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig

# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode,macos,go
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode,macos,go

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

### Go Patch ###
/vendor/
/Godeps/

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

# Support for Project snippet scope
.vscode/*.code-snippets

# Ignore code-workspaces
*.code-workspace

# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,macos,go

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)

Expand Down
21 changes: 21 additions & 0 deletions test/testProjects/containerapps-albumapi-go/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Microsoft Corporation.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
3 changes: 3 additions & 0 deletions test/testProjects/containerapps-albumapi-go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Azure Container Apps Album API

Originally adapted from [this](https://github.com/azure-samples/containerapps-albumapi-go) repository, the companion repository for the [Azure Container Apps code-to-cloud quickstart](https://learn.microsoft.com/en-us/azure/container-apps/quickstart-code-to-cloud?tabs=bash%2Cgo&pivots=with-dockerfile).
24 changes: 24 additions & 0 deletions test/testProjects/containerapps-albumapi-go/src/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
README.md
19 changes: 19 additions & 0 deletions test/testProjects/containerapps-albumapi-go/src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Dockerfile References: https://docs.docker.com/engine/reference/builder/

# Start from the latest golang base image
FROM golang:latest

# Set the Current Working Directory inside the container
WORKDIR /app

# Add files to app folder
ADD . /app

# Build the Go app
RUN go build -o main .

# Expose ports to the outside world
EXPOSE 8080

# Command to run the executable
CMD ["./main"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package album

import (
"album-service/models"
"encoding/json"
"fmt"
"net/http"
)

func init() {
fmt.Println("Initialize - album package")
}

// Get function
func Get(w http.ResponseWriter, r *http.Request) {

// serialize data
j, _ := json.Marshal(models.GetAlbums())

// set response
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
8 changes: 8 additions & 0 deletions test/testProjects/containerapps-albumapi-go/src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module album-service

go 1.17

require (
github.com/gorilla/mux v1.8.0
github.com/rs/cors v1.8.2
)
4 changes: 4 additions & 0 deletions test/testProjects/containerapps-albumapi-go/src/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
50 changes: 50 additions & 0 deletions test/testProjects/containerapps-albumapi-go/src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"album-service/controllers/album"
"fmt"
"log"
"net/http"
"os"
"strings"

"github.com/gorilla/mux"
"github.com/rs/cors"
)

func main() {

// router implementation
router := mux.NewRouter()

// adding cors
c := cors.AllowAll()
handler := c.Handler(router)

port, ok := os.LookupEnv("PORT")
if !ok {
port = "8080"
}

router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Access /albums to see the list of albums")
})

router.HandleFunc("/albums", album.Get).Methods("GET")

// listen and serve
log.Printf("will listen on %v\n", port)
if err := http.ListenAndServe(":" + port, trailingSlashHandler(handler)); err != nil {
log.Fatalf("unable to start http server, %s", err)
}
}

func trailingSlashHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
}
next.ServeHTTP(w, r)
})
}
Loading

0 comments on commit 48c92ef

Please sign in to comment.