Skip to content

add unit testing sesion-11 #24

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ require (
github.com/jackc/pgconn v1.13.0
github.com/jackc/pgx/v4 v4.17.2
github.com/labstack/echo v3.3.10+incompatible
github.com/pkg/sftp v1.13.5
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
github.com/swaggo/http-swagger v1.3.3
github.com/swaggo/swag v1.8.7
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c
google.golang.org/grpc v1.50.1
google.golang.org/protobuf v1.28.1
Expand All @@ -27,6 +30,7 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/beevik/etree v1.1.0 // indirect
github.com/crewjam/httperr v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
Expand All @@ -44,6 +48,7 @@ require (
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.10.5 // indirect
Expand All @@ -52,16 +57,17 @@ require (
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russellhaering/goxmldsig v1.2.0 // indirect
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/tools v0.1.12 // indirect
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFF
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
Expand Down Expand Up @@ -162,6 +164,8 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.5 h1:a3RLUqkyjYRtBTZJZ1VRrKbN3zhuPLlUc3sphVz81go=
github.com/pkg/sftp v1.13.5/go.mod h1:wHDZ0IZX6JcBYRK1TH9bcVq8G7TLpVHYIGJRFnmPfxg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down Expand Up @@ -230,6 +234,7 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
Expand Down Expand Up @@ -263,11 +268,13 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
28 changes: 28 additions & 0 deletions session-11/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"errors"
"fmt"
)

func main() {
fmt.Println(LuasPersegi(4))
}

func LuasPersegi(sisi int) int {
return sisi * sisi
}

func Register(username, password string) error {
if username == "" {
return errors.New("username tidak boleh kosong")
}
if password == "" {
return errors.New("password tidak boleh kosong")
}

// ceritanya masukin ke db

// kalo sukses return nil
return nil
}
33 changes: 33 additions & 0 deletions session-11/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestLuasPersegi(t *testing.T) {
t.Run("case sisi 4", func(t *testing.T) {
luas := LuasPersegi(4)
require.Equal(t, 16, luas)
})
t.Run("case sisi 6", func(t *testing.T) {
luas := LuasPersegi(6)
require.Equal(t, 36, luas)
})
}

func TestRegister(t *testing.T) {
t.Run("case username kosong", func(t *testing.T) {
err := Register("", "password")
require.Error(t, err)
})
t.Run("case password kosong", func(t *testing.T) {
err := Register("username", "")
require.Error(t, err)
})
t.Run("case sukses", func(t *testing.T) {
err := Register("username", "password")
require.NoError(t, err)
})
}
124 changes: 124 additions & 0 deletions session-7/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package main

import (
"io"
"log"
"os"

"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)

func main() {
const SSH_ADDRESS = "0.0.0.0:22"
const SSH_USERNAME = "user"
const SSH_PASSWORD = "password"

sshConfig := &ssh.ClientConfig{
User: SSH_USERNAME,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{
ssh.Password(SSH_PASSWORD),
},
}

// dial ssh
client, err := ssh.Dial("tcp", SSH_ADDRESS, sshConfig)
if client != nil {
defer client.Close()
}
if err != nil {
log.Fatal("Failed to dial. " + err.Error())
}

// create session
session, err := client.NewSession()
if err != nil {
log.Fatal("Failed to create session. " + err.Error())
}
//session.Stdin = os.Stdin
session.Stdout = os.Stdout
session.Stderr = os.Stderr

// test session
stdinBuf, _ := session.StdinPipe()
if err := session.Shell(); err != nil {
panic(err)
}
stdinBuf.Write([]byte("echo hello\n"))
stdinBuf.Write([]byte("ls -l ~/\n"))
//err = session.Run("ls -l ~/")
//if err != nil {
// log.Fatal("Command execution error. " + err.Error())
//}

// test StdinPipe
//var stdout, stderr bytes.Buffer
//session.Stdout = &stdout
//session.Stderr = &stderr
//
//stdin, err := session.StdinPipe()
//if err != nil {
// log.Fatal("Error getting stdin pipe. " + err.Error())
//}
//
//err = session.Start("/bin/bash")
//if err != nil {
// log.Fatal("Error starting bash. " + err.Error())
//}
//
//commands := []string{
// "cd /where/is/the/path",
// "cd src/myproject",
// "ls",
// "exit",
//}
//for _, cmd := range commands {
// if _, err = fmt.Fprintln(stdin, cmd); err != nil {
// log.Fatal(err)
// }
//}
//
//err = session.Wait()
//if err != nil {
// log.Fatal(err)
//}
//
//outputErr := stderr.String()
//fmt.Println("============== ERROR")
//fmt.Println(strings.TrimSpace(outputErr))
//
//outputString := stdout.String()
//fmt.Println("============== OUTPUT")
//fmt.Println(strings.TrimSpace(outputString))

// test sftp
sftpClient, err := sftp.NewClient(client)
if err != nil {
log.Fatal("Failed create client sftp client. " + err.Error())
}

//err = session.Run("touch ~/test-file.txt")
//if err != nil {
// log.Fatal("Command execution error. " + err.Error())
//}
//session.Close()
fDestination, err := sftpClient.Create("/home/user/test-file.txt")
if err != nil {
log.Fatal("Failed to create destination file. " + err.Error())
}

fSource, err := os.Open("/home/ibam/Documents/code/golang-intermediate/session-7/test-file.txt")
if err != nil {
log.Fatal("Failed to read source file. " + err.Error())
}

_, err = io.Copy(fDestination, fSource)
if err != nil {
log.Fatal("Failed copy source file into destination file. " + err.Error())
}

log.Println("File copied.")

// create new session
}
1 change: 1 addition & 0 deletions session-7/test-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hai