Skip to content

Commit

Permalink
test: optimize the program, improve the coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jun 12, 2022
1 parent 8e375ed commit d7db5a7
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 52 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.aptcache
cachedata
apt-proxy
last-cid
coverage.out
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ You can safely use it instead of [apt-cacher-ng](https://www.unix-ag.uni-kl.de/~
- Binaries
- Docker

## Development

coverage:

```bash
# go test -cover ./...

PASS
coverage: 86.7% of statements
ok github.com/soulteary/apt-proxy 0.793s
```

View coverage report:

```
# go test -coverprofile=coverage.out ./...
PASS
coverage: 86.7% of statements
ok github.com/soulteary/apt-proxy 0.485s
# go tool cover -html=coverage.out
```



### (WIP) Development

```bash
Expand Down
41 changes: 40 additions & 1 deletion apt-proxy.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
package main

import (
"flag"

"github.com/soulteary/apt-proxy/cli"
"github.com/soulteary/apt-proxy/linux"
)

const (
DEFAULT_HOST = "0.0.0.0"
DEFAULT_PORT = "3142"
DEFAULT_CACHE_DIR = "./.aptcache"
DEFAULT_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"
DEFAULT_TYPE = linux.UBUNTU
DEFAULT_DEBUG = false
)

var version string

func parseFlags() (appFlags cli.AppFlags) {
var (
host string
port string
types string
)
flag.StringVar(&host, "host", DEFAULT_HOST, "the host to bind to")
flag.StringVar(&port, "port", DEFAULT_PORT, "the port to bind to")
flag.StringVar(&types, "type", DEFAULT_TYPE, "select the type of system to cache: ubuntu/debian")
flag.BoolVar(&appFlags.Debug, "debug", DEFAULT_DEBUG, "whether to output debugging logging")
flag.StringVar(&appFlags.CacheDir, "cachedir", DEFAULT_CACHE_DIR, "the dir to store cache data in")
flag.StringVar(&appFlags.Mirror, "mirror", DEFAULT_MIRROR, "the mirror for fetching packages")
flag.Parse()

if types != linux.UBUNTU && types != linux.DEBIAN {
types = linux.UBUNTU
}

appFlags.Types = types
appFlags.Listen = host + ":" + port
appFlags.Version = version

return appFlags
}

func main() {
cli.Parse()
cli.Parse(parseFlags())
}
29 changes: 29 additions & 0 deletions apt-proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"testing"
)

func TestParseFlags(t *testing.T) {
flags := parseFlags()

if flags.Debug != DEFAULT_DEBUG {
t.Fatal("Default option `Debug` value mismatch")
}

if flags.Listen != (DEFAULT_HOST + ":" + DEFAULT_PORT) {
t.Fatal("Default option `Listen` value mismatch")
}

if flags.Types != DEFAULT_TYPE {
t.Fatal("Default option `Types` value mismatch")
}

if flags.Mirror != DEFAULT_MIRROR {
t.Fatal("Default option `Mirror` value mismatch")
}

if flags.CacheDir != DEFAULT_CACHE_DIR {
t.Fatal("Default option `CacheDir` value mismatch")
}
}
66 changes: 17 additions & 49 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,77 +1,45 @@
package cli

import (
"flag"
"log"
"net/http"

"github.com/soulteary/apt-proxy/linux"
"github.com/soulteary/apt-proxy/pkgs/httpcache"
"github.com/soulteary/apt-proxy/pkgs/httplog"
"github.com/soulteary/apt-proxy/proxy"
)

const (
DEFAULT_HOST = "0.0.0.0"
DEFAULT_PORT = "3142"
DEFAULT_CACHE_DIR = "./.aptcache"
DEFAULT_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"
DEFAULT_TYPE = linux.UBUNTU
DEFAULT_DEBUG = false
)

var (
version string
listen string
mirror string
types string
cacheDir string
debug bool
)

func init() {
var (
host string
port string
)
flag.StringVar(&host, "host", DEFAULT_HOST, "the host to bind to")
flag.StringVar(&port, "port", DEFAULT_PORT, "the port to bind to")
flag.BoolVar(&debug, "debug", DEFAULT_DEBUG, "whether to output debugging logging")
flag.StringVar(&mirror, "mirror", DEFAULT_MIRROR, "the mirror for fetching packages")
flag.StringVar(&types, "type", DEFAULT_TYPE, "select the type of system to cache: ubuntu/debian")
flag.StringVar(&cacheDir, "cachedir", DEFAULT_CACHE_DIR, "the dir to store cache data in")
flag.Parse()

if types != linux.UBUNTU && types != linux.DEBIAN {
types = linux.UBUNTU
}

listen = host + ":" + port
type AppFlags struct {
Debug bool
Version string
CacheDir string
Mirror string
Types string
Listen string
}

func Parse() {

log.Printf("running apt-proxy %s", version)
func Parse(appFlags AppFlags) {
log.Printf("running apt-proxy %s", appFlags.Version)

if debug {
if appFlags.Debug {
log.Printf("enable debug: true")
httpcache.DebugLogging = true
}

cache, err := httpcache.NewDiskCache(cacheDir)
cache, err := httpcache.NewDiskCache(appFlags.CacheDir)
if err != nil {
log.Fatal(err)
}

ap := proxy.NewAptProxyFromDefaults(mirror, types)
ap := proxy.NewAptProxyFromDefaults(appFlags.Mirror, appFlags.Types)
ap.Handler = httpcache.NewHandler(cache, ap.Handler)

logger := httplog.NewResponseLogger(ap.Handler)
logger.DumpRequests = debug
logger.DumpResponses = debug
logger.DumpErrors = debug
logger.DumpRequests = appFlags.Debug
logger.DumpResponses = appFlags.Debug
logger.DumpErrors = appFlags.Debug
ap.Handler = logger

log.Printf("proxy listening on %s", listen)
log.Fatal(http.ListenAndServe(listen, ap))
log.Printf("proxy listening on %s", appFlags.Listen)
log.Fatal(http.ListenAndServe(appFlags.Listen, ap))
}

0 comments on commit d7db5a7

Please sign in to comment.